[英]Binding inner ContentControl control to parent ItemsControl item
我想我幾乎得到了它,但正在為最終解決方案而苦苦掙扎。
ItemsControl
- 一切正常。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>
您可能想嘗試獲取ContentControl
的DataContext
。
<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.