【发布时间】: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