繁体   English   中英

将内部 ContentControl 控件绑定到父 ItemsControl 项

[英]Binding inner ContentControl control to parent ItemsControl item

我想我几乎得到了它,但正在为最终解决方案而苦苦挣扎。

  • 我有具有 N 个项目的BoardList ItemsControl - 一切正常。
  • 我有一个包含某些字段的内部Eeprom ContentControl - 一切正常,除了我必须将按钮的Tag绑定到ItemsControl项目。 在后面的代码中,我会将这个TagBoard对象并执行各种操作。

如您所见,我尝试使用RelativeSource但它带我进入实际的ItemsControl ,而不是项目本身。 这可能是我想念的愚蠢的东西,但我就是无法得到它。 任何帮助表示赞赏。 谢谢!

<!--List of "Board" items-->
<ItemsControl ItemsSource="{Binding BoardList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <GroupBox>
                <!--Eeprom is an object within "Board" item-->
                <ContentControl Content="{Binding Eeprom}">
                    <ContentControl.ContentTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBox Text="{Binding IdPage}"/>
                                <Button Tag="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}}" Click="Button_Click"/>
                            </StackPanel>
                        </DataTemplate>
                    </ContentControl.ContentTemplate>
                </ContentControl>
            </GroupBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

完整版(我无意中留下了重要的容器):

<!--List of "Board" items-->
<ItemsControl ItemsSource="{Binding ContrSysAccessor.IBoardList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!--Eeprom is an object within "Board" item-->
            <ContentControl Content="{Binding Eeprom}">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <materialDesign:ColorZone>
                            <materialDesign:PopupBox>
                                <StackPanel>
                                    <TextBox Text="{Binding IdPage}"/>
                                    <Button Tag="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext}" Click="Button_Click"/>
                                </StackPanel>
                            </materialDesign:PopupBox>
                        </materialDesign:ColorZone>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

您可能想尝试获取ContentControlDataContext

<Button Tag="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext}" Click="Button_Click"/>

控件层次结构中有多个从ContentControl派生的控件:

  • ContentControl (你的)
    • materialDesign:ColorZone
      • materialDesign:PopupBox
        • materialDesign:Card (未显示)

这就是为什么您的绑定不起作用。 它会在遍历父控件时返回第一个控件的数据上下文。 该控件是materialDesign:Card及其数据上下文Eeprom

如果您指定层次结构中的哪个ContentControl ,您可以使您的绑定工作。 这是由AncestorLevel定义的。 您的目标ContentControl是父层次结构中的第四个,因此指定4

<Button Tag="{Binding RelativeSource={RelativeSource AncestorType=ContentControl, AncestorLevel=4}, Path=DataContext}" Click="Button_Click"/>

此处有效的RelativeSource绑定的替代方法是在目标ContentControl上设置x:Name并在绑定中使用ElementName引用它。

<ItemsControl ItemsSource="{Binding Parent}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <!--Eeprom is an object within "Board" item-->
         <ContentControl x:Name="MyContentControl" Content="{Binding Text}">
            <ContentControl.ContentTemplate>
               <DataTemplate>
                  <materialDesign:ColorZone>
                     <materialDesign:PopupBox>
                        <StackPanel>
                           <TextBox Text="{Binding Mode=OneWay}"/>
                           <Button Tag="{Binding DataContext, ElementName=MyContentControl}" Click="Button_Click"/>
                        </StackPanel>
                     </materialDesign:PopupBox>
                  </materialDesign:ColorZone>
               </DataTemplate>
            </ContentControl.ContentTemplate>
         </ContentControl>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM