WPF ListView中为RadioButton、CheckBox绑定int类型属性实现勾选
现存问题梳理
跑通当前代码会发现三个直接卡功能生效的问题:
- 单元格内的RadioButton、CheckBox没有关联任何
IsChecked绑定,和实体字段完全没有数据通路 - WPF中控件的
IsChecked属性是可空布尔类型,和实体里用int存储0/1表示选中状态的写法没有原生适配的转换逻辑,直接绑会报类型不匹配 - Salesman实体的属性没有实现变更通知,手动点击控件修改选中状态时,后台字段值不会同步更新
落地实现步骤
1. 自定义int与可空布尔的双向转换器
因为没有原生的类型转换,需要写个简单的转换器实现双向映射:
public class IntToBoolConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // 从int转到控件的选中状态:值为1则选中 if (value is int intVal) return intVal == 1; return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { // 从控件的选中状态转回int:选中为1,未选中为0 if (value is bool boolVal) return boolVal ? 1 : 0; return 0; } }
把转换器注册到当前窗体/用户控件的资源字典中,注意把local替换成项目里转换器对应的命名空间:
<UserControl.Resources> <local:IntToBoolConverter x:Key="IntToBool" /> </UserControl.Resources>
2. 修正ListView的绑定指向
当前代码里在属性变更事件中直接给ListView的DataContext赋值为vm.Salesmen集合,和原来写的ItemsSource="{Binding}"逻辑重复,很容易出现绑定错位。调整为两层逻辑:
- 把ListView的
ItemsSource明确绑定到ViewModel里的Salesmen集合属性
<ListView x:Name="lvSalesmen" ItemsSource="{Binding Salesmen}" Margin="311,32,0,244" Grid.ColumnSpan="5">
- 把ListView的
DataContext直接指向完整的ViewModel实例,而不是单独的集合:
private void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e) { lvSalesmen.DataContext = vm; }
3. 给单元格控件加上正确的双向绑定
修改DataTemplate里的控件定义,关联对应的实体字段,引入之前写的转换器:
- IsResponsible列的RadioButton:保留原来的GroupName设置,绑定
IsChecked
<RadioButton GroupName="IsResponsible" IsChecked="{Binding IsResponsible, Converter={StaticResource IntToBool}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
- IsSecondary列的CheckBox:不需要分组,直接绑定
IsChecked
<CheckBox IsChecked="{Binding IsSecondary, Converter={StaticResource IntToBool}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
这里必须开启TwoWay绑定模式,不然只会在数据加载时根据int值显示选中状态,用户手动操作控件后,后台的int值不会同步更新。
4. 给Salesman实体加上属性通知机制
当前Salesman类的属性都是无通知的自动属性,要实现绑定的实时同步,需要让实体实现INotifyPropertyChanged接口,在属性值变更时主动通知绑定的控件:
public class Salesman : INotifyPropertyChanged { private int _id; private string _firstName; private string _lastName; private int _isResponsible; private int _isSecondary; public int Id { get => _id; set { _id = value; OnPropertyChanged(); } } public string FirstName { get => _firstName; set { _firstName = value; OnPropertyChanged(); } } public string LastName { get => _lastName; set { _lastName = value; OnPropertyChanged(); } } public int IsResponsible { get => _isResponsible; set { _isResponsible = value; OnPropertyChanged(); } } public int IsSecondary { get => _isSecondary; set { _isSecondary = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged = delegate { }; protected void OnPropertyChanged([CallerMemberName] string propName = "") { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } }
最终效果
- 数据加载时,实体的
IsResponsible值为1的行,对应RadioButton会自动显示为选中状态;因为统一设置了GroupName,同一时间整个列表只会有一个RadioButton处于选中状态,选中其他RadioButton时,会自动同步把之前选中行的IsResponsible值更新为0,当前行更新为1 - 实体的
IsSecondary值为1的行,对应CheckBox会自动显示为选中状态;因为CheckBox没有分组限制,支持同时选中多行的CheckBox,每次选中/取消操作都会自动把对应的IsSecondary值在0和1之间同步切换
内容的提问来源于stack exchange,提问作者TheSteelDuck
相关产品推荐
相关产品推荐

