使用 WPF MVVM 触发 ListBox 的 DataTemplate 中存在的控件的事件

发布于 2024-12-13 09:14:17 字数 1031 浏览 0 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2024-12-20 09:14:17

您在 EventToCommand Command 绑定表达式中缺少开头 {

应该是:

<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GroupCheckBoxCheckCommand}"></cmd:EventToCommand>

You are missing the opening { in your EventToCommand Command binding expression:

Should be:

<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GroupCheckBoxCheckCommand}"></cmd:EventToCommand>
独守阴晴ぅ圆缺 2024-12-20 09:14:17

EventToCommand 对我来说一直感觉不必要的复杂。为什么你希望这是一个命令?

为什么不直接从绑定到 IsCheckedExcludeVal 属性的 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 to IsChecked? Or have your view model watch its own PropertyChanged event and look for when the ExcludeVal property changes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文