I make this column for ListView with TextBox'ом inside and try to bind the Width TextBox'a property to the field of the same name GridViewColumn'a .

 <GridViewColumn Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBox Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GridViewColumn}, Path=Width}" Text="SomeText"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 

As a result, no binding occurs. The following message is displayed in Output:

System.Windows.Data Error: 4; Find not Finding Resistance, AncestorType = 'System.Windows.Controls.GridViewColumn', AncestorLevel = '1' '. BindingExpression: Path = Width; DataItem = null; target element is 'TextBox' (Name = ''); target property is 'Width' (type 'Double')

If you specify a name for the GridViewColumn , and bind via ElementName , then everything works. Why does the first option fail?

    1 answer 1

    This does not work because the FindAncestor search goes up the visual tree to the encapsulation boundary. DataTemplate is such a boundary, because it can be defined separately, for example, in resources.

    • And how to be in a situation? There are ways to solve without specifying the name of the column? - Donil
    • @Danil: Not sure if it will work, but: if you put the DataTemplate in the root of the Grid , will it not automatically have full width? (Check by setting the color background.) Then you can snap to its width. - VladD
    • @Landland: unfortunately not. Grid gets the width of the content - Donil