2010-10-21 138 views
0

当使用具有ItemsSource的ItemsControl时,我在使用UserControl内的数据绑定时遇到了一些麻烦。我的Eventtrigger从未被调用过。MVVM嵌套数据绑定

我认为问题是,当我把我的eventtrigger在该行:

<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" /> 

它试图找到的CheckBox收集检查事件因为我已经把我的ItemsSource,而应该看它父母。我一直在寻找解决方案的天,但他们都没有工作。

我的代码如下所示:

<Grid x:Name="layoutroot"> 
      <ItemsControl x:Name="itemcontrol" ItemsSource="{Binding CheckBoxes}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Vertical"></StackPanel> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <s:SurfaceCheckBox Background="White" Foreground="White"> 
          <i:Interaction.Triggers> 
           <i:EventTrigger EventName="Checked"> 
            <cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" /> 
           </i:EventTrigger> 
          </i:Interaction.Triggers> 
         </s:SurfaceCheckBox> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </Grid> 

当我尝试下面的代码它的工作原理完全如预期:

<Grid x:Name="layoutroot"> 
    <s:SurfaceCheckBox Background="White" Foreground="White" Content="{Binding Content}" > 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Checked"> 
       <cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </s:SurfaceCheckBox> 
</Grid> 

但我真的需要一个ItemsControl内部的这种行为一组的ItemsSource。

任何想法?

回答

1

ItemsControl的内部绑定放置在集合中的当前项目上。你需要做的是找出父母,并从那里绑定。

试试这个从ItemsControl内,更换MyUserControlName

<cmd:EventToCommand Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyUserControlName} }, Path=DataContext.Checked}" /> 
+0

感谢您的答复。我发现我的复选框上还有另一层,这就是为什么我的事件没有被触发。 – Marc 2010-10-24 09:50:25