【问题标题】:Binding inner ContentControl control to parent ItemsControl item将内部 ContentControl 控件绑定到父 ItemsControl 项
【发布时间】:2020-10-05 15:27:51
【问题描述】:

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

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

如您所见,我尝试使用 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>

【问题讨论】:

    标签: wpf binding itemscontrol contentcontrol


    【解决方案1】:

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

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

    这就是您的绑定不起作用的原因。它将在遍历父控件时返回 first 控件的数据上下文。这个控件是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>
    

    【讨论】:

    • 谢谢!了解这个 MD 库会有所帮助,因为我忘记了 PopupBox 是用卡片构建的。无论如何,非常有用的迷你教程。谢谢!
    【解决方案2】:

    您可能想尝试获取ContentControlDataContext

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

    【讨论】:

    • Keithernet,谢谢!您的回答有效(在我将结构调整为我最初发布的内容之后)。我在这里对其进行了简化,认为它会使它更干净,并错过了产生差异的容器。我刚刚编辑了具有完整结构的原始帖子,但仍然无法正常工作。如您所见,有 2 个内部自定义 ContentControls,我知道这让我很头疼,因为如果我删除它们,您的解决方案就可以工作。你能告诉我如何解决这个问题吗?谢谢你。我知道有一个FindAncestor 模式,但我不能让它工作。
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2010-12-16
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多