我是WPF的新手并坚持这个问题:
我想在“点击”列中为所选行启用我的DataGridComboBoxColumns的多次检查/取消选中。
我的DataGrid看起来像
<DataGrid ItemsSource="{Binding SelectedListRTEntity.Properties}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Visibility="Hidden" Binding="{Binding ID}" />
<DataGridCheckBoxColumn Header="Generate" Width="100" Binding="{Binding Generate}" />
<DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}" />
<DataGridCheckBoxColumn Header="Is nullable" Width="100" Binding="{Binding IsNullable}" />
<DataGridTextColumn Header="Basic type" Width="100" Binding="{Binding BasicType}" />
<DataGridCheckBoxColumn Header="UseCustomGetterSetter" Width="100" Binding="{Binding UseCustomGetterSetter}" />
</DataGrid.Columns>
非常重要的是,多重检查会检查右栏中的复选框。
我不知道如何处理这个问题。 有没有办法在没有代码隐藏的情况下解决这个问题?
编辑:也许这个截图有助于澄清,我的意思是:
然后在SHIFT中取消选中它们并单击其中一个复选框
答案 0 :(得分:1)
不编写代码就不可能达到预期的效果。您需要拥有绑定到Checkbox的属性才能通知。因此,为这些属性实现INotifyPropertyChanged。
执行以下更改后,当我选中/取消选中任何选定行中的任何“生成”复选框时,“生成”字段的所有复选框对所选行都相同。未选择的行保持不受影响。
<DataGrid x:Name="myGrid" ItemsSource="{Binding SelectedListRTEntity.Properties}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Visibility="Hidden" Binding="{Binding ID}" />
<DataGridTemplateColumn Width="100" Header="Generate">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Generate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Checked="GenerateCheckBox_Checked" Unchecked="GenerateCheckBox_Unchecked"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}" />
<DataGridCheckBoxColumn Header="Is nullable" Width="100" Binding="{Binding IsNullable}" />
<DataGridTextColumn Header="Basic type" Width="100" Binding="{Binding BasicType}" />
<DataGridCheckBoxColumn Header="UseCustomGetterSetter" Width="100" Binding="{Binding UseCustomGetterSetter}" />
</DataGrid.Columns>
</DataGrid>
您的物业类:
public class Property:INotifyPropertyChanged
{
public String ID { get; set; }
public String Name { get; set; }
public bool IsNullable { get; set; }
public String BasicType { get; set; }
public bool UseCustomGetterSetter { get; set; }
private bool _generate;
public bool Generate {
get { return _generate; }
set { _generate = value; OnPropertyChanged(this, new PropertyChangedEventArgs("Generate")); }
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(Object sender, PropertyChangedEventArgs e) {
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
在代码隐藏
中 private void GenerateCheckBox_Checked(object sender, RoutedEventArgs e)
{
IList items = myGrid.SelectedItems;
foreach (Property p in items)
{
p.Generate = true;
}
}
private void GenerateCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
IList items = myGrid.SelectedItems;
foreach (Property p in items)
{
p.Generate = false;
}
}
答案 1 :(得分:0)