我有一个复选框,我打算在其上实现“全选”功能 .
我认为最简单的实现是使用DataTrigger修改我的DataTemplate以将IsChecked属性更改为true / false .
<UserControl.Resources>
<DataTemplate x:Key="MyTemplate">
<Grid>
<CheckBox x:Name="Selection" IsChecked="{Binding Selected}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=SelectAll, Path=IsChecked}" Value="true">
<Setter TargetName="Selection" Property="DataContext.Selected" Value="true" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=SelectAll, Path=IsChecked}" Value="false">
<Setter TargetName="Selection" Property="IsChecked" Value="false" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</UserControl.Resources>
<Grid>
<CheckBox Name="SelectAll />
<ListView Name="lvSteps" ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding MyList}" />
</Grid>
但是,这会将设置为DataContext属性“Selected”的CheckBox的绑定覆盖为DataTrigger中设置的bool值 .
考虑它是完全合理的 .
So my question is, can I change the value of DataContext property within a DataTrigger via Setter?
所以我可以将CheckBox绑定到'Selected',并更改DataTrigger的Setter中的'Selected'值?
1 回答
你可能会以错误的方式思考它 .
SelectAll
是你的容器元素(比如说ListVM)的一部分,哪个实习生持有你的对象(ListItemVM),其中包含Selected
所以如果你检查SelectAll
你可以并且应该迭代你的元素并将Selected
设置为true并且这个项目在那里使用INotifyPropertyChanged来通知你的CheckBoxEdit for Comment
没有必要挂钩
SelectAll
的事件只是绑定到它并在Set-Part中做你的事情另外你为什么要在xaml中这样做?
因为你可以使用接口并以一种你可以在许多地方使用它而没有代码冗余的方式将其分解出来,你也可以在代码中从你能够访问“ListVM”的地方开始