Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WPF MVVM激发ListBox的DataTemplate中存在的控件的事件_C#_Wpf_Mvvm - Fatal编程技术网

C# 使用WPF MVVM激发ListBox的DataTemplate中存在的控件的事件

C# 使用WPF MVVM激发ListBox的DataTemplate中存在的控件的事件,c#,wpf,mvvm,C#,Wpf,Mvvm,有一个属性绑定到我的Listbox数据模板的复选框,当该属性设置为true时,Listbox的复选框也被选中,现在我想通过事件执行一个命令,但它不会触发。请帮忙EventToCommand总是让我觉得不必要的复杂。你为什么要把它当作命令 为什么不从绑定到IsChecked的ExcludeVal属性的setter中调用命令的execute方法呢?或者让您的视图模型监视自己的属性更改事件,并在ExcludeVal属性更改时查找?您缺少事件命令绑定表达式中的开头{: 应该是: <ListBox

有一个属性绑定到我的Listbox数据模板的复选框,当该属性设置为true时,Listbox的复选框也被选中,现在我想通过事件执行一个命令,但它不会触发。请帮忙

EventToCommand
总是让我觉得不必要的复杂。你为什么要把它当作命令


为什么不从绑定到
IsChecked
ExcludeVal
属性的setter中调用命令的execute方法呢?或者让您的视图模型监视自己的
属性更改
事件,并在
ExcludeVal
属性更改时查找?

您缺少
事件命令
绑定表达式中的开头
{

应该是:

<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>


实际上我不能这样做,因为我的ExcudeVal属性是POCO实体中的一个属性。所以我不能这样做。我希望您在绑定表达式中使用的ViewModel的任何属性都至少引发OnPropertyChanged事件。如果没有,那么我会将该属性包装在您的ViewModel中的一个新属性中,该属性会引发事件。然后,最好将您的POCO封装在一个简单的ViewModel类中,比如TripAttributeOptions ViewModel。或者,您可以尝试CommandReference类(只需谷歌一下)。我用它来做类似的事情。它允许您引用数据模板之外的命令(比如在窗口的数据上下文中)通过StaticResource而不是凌乱的相对源绑定。
<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GroupCheckBoxCheckCommand}"></cmd:EventToCommand>