2011-11-03 78 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

像鷹一樣的眼睛... –