[英]Checkbox Command Not Firing When Initially Unchecked
我在数据网格中有一列复选框。
<DataGridTemplateColumn CanUserResize="False" Header="" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource CheckBoxSelectTypeStyle}" IsChecked="{Binding Path=Selected}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我有一个必须取消选中和检查的命令。
<Style x:Key="CheckBoxSelectTypeStyle" TargetType="{x:Type CheckBox}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Command" Value="{Binding DataContext.CheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Command" Value="{Binding DataContext.UncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Trigger>
</Style.Triggers>
</Style>
我的问题是,如果复选框以未选中的方式启动并且您选中一个,则不会触发任何命令(这是问题所在)。 如果您取消选中相同的复选框,则会触发取消选中命令(正如预期的那样)。 如果再次检查相同的复选框,则会触发check命令(如预期的那样)。 此时所有内容都可以正常运行,但其他内容仍然存在同样的问题。
如果复选框以选中状态启动,则可以正常工作。 我的问题是当复选框启动为未选中时,如何激活命令。 我找不到任何不工作的理由。
回应建议:
我试图为Null
添加一个触发器,但它有完全相同的问题,没有任何改变。
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Command" Value="{Binding DataContext.CheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Command" Value="{Binding DataContext.UncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Command" Value="{Binding DataContext.UncheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</Trigger>
</Style.Triggers>
ViewModel代码
public ICommand CheckedCommand { get; set; }
public ICommand UncheckedCommand { get; set; }
CheckedCommand = new RelayCommand<IList>(Checked);
UncheckedCommand = new RelayCommand<IList>(Unchecked);
private void Checked(IList selectedItems)
{
ChangedChecked(selectedItems, true);
}
private void Unchecked(IList selectedItems)
{
ChangedChecked(selectedItems, false);
}
private void ChangedChecked(IList selectedItems, bool selected)
{
if (selectedItems.HasValue())
foreach (var item in selectedItems)
if(item is SelectedTypeModel) (item as SelectedTypeModel).Selected = selected;
}
RelayCommand实现ICommand。 正如我之前提到的,当复选框以未选中的方式启动时,不会调用Checked
方法,但是当它被选中,取消选中并再次检查时,或者如果它被选中并且未选中, 则会调用它。
我正在尝试做什么
我有一个带有一列CheckBox的DataGrid。 我希望能够突出显示多行,然后选中/取消选中其中一个选定行中的CheckBox,并将所有其他行更新为相同。 我还有一个datagrid的关键字过滤器,因此DataGrid绑定到ListCollectionView。
输出信息
我启用了数据绑定的调试信息并获取此消息:
System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值; 使用默认值。 BindingExpression:路径=选定; 的DataItem = NULL; target元素是'CheckBox'(Name =''); target属性是'IsChecked'(类型'Nullable`1')
我仍然不确定如何使用此信息来纠正问题。
解
我没有修复我原来的问题,我仍然不知道为什么它不能正常工作,但我已经使用了另一种方法,具有相同的预期结果。 这是更改的代码:
<Style x:Key="CheckBoxSelectTypeStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Command" Value="{Binding DataContext.CheckedChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter">
<Setter.Value>
<MultiBinding Converter="{StaticResource SelectedItemsCheckedMultiValueConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" Path="SelectedItems"/>
<Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
public ICommand CheckedChangedCommand { get; set; }
CheckedChangedCommand = new RelayCommand<Tuple<IList, bool>>(CheckedChanged);
解决方案我认为你可以在不需要触发器的情况下实现你的目标(我已经在小样本应用程序中测试了这个解决方案):所以你的风格看起来像这样:
<Style x:Key="CheckBoxSelectTypeStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Command" Value="{Binding DataContext.CheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<Setter Property="CommandParameter" Value="{Binding Path=SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
...
视图模型中只有一个命令。 并且作为参数,该命令将接收所选项目的列表。 因此,只缺少一件事 - 用户是否要检查或取消选中复选框。 目前我可以想到一种可能的方式将此信息与SelectedItems一起传递 - 使用自定义转换器编写MultiBinding,它会将selectedItems和IsChecked的当前值放在像Tuple <...,bool>这样的东西中
<Setter Property="CommandParameter">
<Setter.Value>
<MultiBinding Converter="{x:Static PackTupleConverter.Instance}">
<Binding RelativeSource="{RelativeSource AncestorType=DataGrid}" Path="SelectedItems"/>
<Binding RelativeSource="{x:Static RelativeSource.Self}" Path="IsChecked"/>
</MultiBinding>
</Setter.Value>
</Setter>
结束
我只需在ListBox中放入相同的复选框即可重现您的问题。 一旦完成第一次我点击复选框 - 命令不要被调用
这是示例窗口xaml:
<Window.Resources>
<Style x:Key="CheckBoxSelectTypeStyle" TargetType="{x:Type CheckBox}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Command" Value="{Binding DataContext.Test, RelativeSource={RelativeSource AncestorType=Window}}" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Command" Value="{Binding DataContext.Test, RelativeSource={RelativeSource AncestorType=Window}}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<ListBox>
<ListBox.Items>
<CheckBox Style="{StaticResource CheckBoxSelectTypeStyle}" />
</ListBox.Items>
</ListBox>
</Grid>
和sode行为:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Test = new DelegateCommand(TestCommand);
}
public ICommand Test { get; set; }
private void TestCommand()
{
}
}
这是我在关于绑定的输出中发现的:
System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值; 使用默认值。 BindingExpression:路径= DataContext.Test; 的DataItem = NULL; target元素是'CheckBox'(Name =''); target属性是'Command'(类型'ICommand')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.