I think I almost got it but struggling with final solution.
- I have BoardList
ItemsControlthat has N number of items - all works. - I have an inner Eeprom
ContentControlthat contains certain fields - everything works except I have to bind button'sTagtoItemsControlitem. In code behind I will cast thisTagto 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
ContentControlin the control hierarchy:ContentControl(yours)materialDesign:ColorZonematerialDesign:PopupBoxmaterialDesign: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:Cardand its data contextEeprom.You can make your binding work, if you specify which
ContentControlin the hierarchy you want. This is defined by theAncestorLevel. Your targetContentControlis the fourth in the parent hierarchy, so specify4.An alternative to a
RelativeSourcebinding, that works here, is setting anx:Nameon your targetContentControland referring to it in the binding withElementName.