I think I almost got it but struggling with final solution.
- I have BoardList
ItemsControl
that has N number of items - all works. - I have an inner Eeprom
ContentControl
that contains certain fields - everything works except I have to bind button'sTag
toItemsControl
item. In code behind I will cast thisTag
to Board object and do all kinds of stuff.
As you can see, I tried using RelativeSource
but it's taking me to an actual ItemsControl
, not an item itself. It's probably something stupid I'm missing but I just can't get it.
Any help appreciated. Thanks!
<!--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>
Complete version (I unintentionally left of important containers):
<!--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>
There are multiple controls derived from
ContentControl
in the control hierarchy:ContentControl
(yours)materialDesign:ColorZone
materialDesign:PopupBox
materialDesign:Card
(not shown)That is why your binding does not work. It will return the data context of the first control while traversing the parent controls. This control is
materialDesign:Card
and its data contextEeprom
.You can make your binding work, if you specify which
ContentControl
in the hierarchy you want. This is defined by theAncestorLevel
. Your targetContentControl
is the fourth in the parent hierarchy, so specify4
.An alternative to a
RelativeSource
binding, that works here, is setting anx:Name
on your targetContentControl
and referring to it in the binding withElementName
.