[英]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.