复选框不应具有相同的名称。
您需要绑定和 IValueConverter:
<stackPanel Name="StudentDetails">
<checkBox Name="Left"
Content="(M)"
IsChecked="{Binding Student.Name, Converter={StaticResource StudentCheckedConverter}, ConverterParameter="M"}" />
<checkBox Name="Left"
Content="(F)"
IsChecked="{Binding Student.Name, Converter={StaticResource StudentCheckedConverter}, ConverterParameter="F"}" />
<Label Content="{Binding Student.Name}" />
</stackPanel>
IValueConverter 看起来像:
public class StudentCheckedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ("M".Equals(parameter))
return "Sam".Equals(value);
if ("F".Equals(parameter))
return "Lucie".Equals(value);
throw new Exception("Unknown parameter or value");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
此外,您需要在 XAML 的资源中公开 StudentCheckedConverter,但我认为一点点谷歌搜索会对您有所帮助。