2011-11-03 80 views
2
<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> 

有一个属性绑定到我的列表框DataTemplate中的复选框,当prperty设置为true,列表框的复选框也得到遏制,现在我想通过事件执行命令但它不会触发。请帮忙火灾使用WPF MVVM控件出现在DataTemplate中的ListBox的事件

回答

0

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

为什么不直接从IsCheckedExcludeVal属性的setter中调用该命令的执行方法呢?或者让您的观看模型观看自己的PropertyChanged事件并查看ExcludeVal属性何时更改?

+0

其实我不能这样做,因为我的ExcudeVal属性是POCO实体的属性。所以我不能做这件事。 –

+0

我希望你的ViewModel的任何属性在Binding表达式中用来至少引发OnPropertyChanged事件。如果没有,那么我会将该属性包装在ViewModel中的一个新属性中,该属性会引发事件。 – CoderDennis

+1

然后,将您的POCO包装到一个简单的ViewModel类中,比如TripAttributeOptionViewModel,这可能是一个好主意。或者,你可以尝试CommandReference类(只是谷歌它)。我用它来做这样的事情。它允许您通过StaticResource而不是混乱的相对源绑定来引用数据模板之外的命令(如在窗口的数据上下文中)。 –

1

你缺少开幕{EventToCommandCommand绑定表达式:

应该是:

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

像鹰一样的眼睛... –