簡體   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