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

时间:2011-11-03 14:08:22

标签: c# wpf mvvm

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

有一个属性绑定到我的Listbox datatemplate的复选框,当该prperty设置为true时,listbox的复选框也被检查,现在我想通过该事件执行命令,但它不会触发。请帮助

2 个答案:

答案 0 :(得分:1)

您错过了{ EventToCommand绑定表达式中的开头Command

应该是:

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

答案 1 :(得分:0)

{p> EventToCommand对我来说总是觉得不必要的复杂。你为什么要这个命令?

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