Bonding grid height using FindAncestor and AncestorLevel for another mesh

My question is how to properly bind the properties of one element to another.

Here is the structure of my code:

  • The data template contains a grid (let it be called a GridA grid), and in the grid I specify an instance of a control called ControlA .

  • ControlA is a UserControl containing a Grid . In ControlA (code-behind), there is user logic that dynamically creates content, but in a nutshell uses a different data template.

  • The data template for ControlA consists of another Grid . I want to bind the Height property for this grid in this data template to the Height Grid property in the data template specified in my first mark above (a grid called GridA ).

Here is the XAML that I used to bind, but essentially it does not work as it does not find the Grid:

 <Grid Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=2}, UpdateSourceTrigger=PropertyChanged}"> </Grid> 

I understand that by setting AncestorLevel=2 , it will use the second β€œfound” occurrence of the type you are looking for, in this case the Grid type. Therefore, in my opinion, he will first find the Grid in ControlA , then continue moving through the tree and find the Grid in the first data template, which is a Grid named GridA . This should be the second occurrence, right?

+6
wpf binding
source share
2 answers

Since you start the search from the second grid, you really need the ancestor level = 1 (which is the default). Note:

 <Grid x:Name="first"> <Grid x:Name="second"> <Grid x:Name="third" Tag="{Binding Name, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=2}}"> <!-- displays "first", not "second" --> <TextBlock Text="{Binding Tag, ElementName=third}"/> </Grid> </Grid> </Grid> 

By the way, your design sounds like it's struggling with the WPF layout system, rather than hugging it. Thus, you are likely to create a lot of unnecessary pain for yourself.

+6
source share

SharedSizeGroup I missing something or can you just use the SharedSizeGroup property for RowDefinitions and set Grid.IsSharedSizeGroup="True" to an external control?

0
source share

All Articles