使用 WPF MVVM 触发 ListBox 的 DataTemplate 中存在的控件的事件
<ListBox Grid.Row="1" Grid.Column="1" Style="{StaticResource BasicListBoxStyle}" ItemsSource="{Binding TripAttributeOptions}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Description}" IsChecked="{Binding ExcludeVal, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand PassEventArgsToCommand="True" Command="Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GroupCheckBoxCheckCommand}"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
有一个属性绑定到我的列表框数据模板的复选框,当该属性设置为 true 时,列表框的复选框也会被选中,现在我想通过事件执行命令,但它不会触发。请帮忙
<ListBox Grid.Row="1" Grid.Column="1" Style="{StaticResource BasicListBoxStyle}" ItemsSource="{Binding TripAttributeOptions}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Description}" IsChecked="{Binding ExcludeVal, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand PassEventArgsToCommand="True" Command="Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GroupCheckBoxCheckCommand}"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
There is a property bind to the checkbox of my Listbox datatemplate, when that prperty set to true, checkbox of listbox also get checked, now i want to execute a command through the event but it won't fire. Kindly help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
EventToCommand
Command
绑定表达式中缺少开头{
:应该是:
You are missing the opening
{
in yourEventToCommand
Command
binding expression:Should be:
EventToCommand
对我来说一直感觉不必要的复杂。为什么你希望这是一个命令?为什么不直接从绑定到
IsChecked
的ExcludeVal
属性的 setter 中调用命令的执行方法呢?或者让您的视图模型监视其自己的PropertyChanged
事件并查找ExcludeVal
属性何时发生更改?EventToCommand
has always felt unnecessarily complex to me. Why do you want this to be a command?Why not just call the command's execute method from within the setter of your
ExcludeVal
property, which is bound toIsChecked
? Or have your view model watch its ownPropertyChanged
event and look for when theExcludeVal
property changes?