I have two datagrids with one column each.
First:
<DataGrid.Columns>
<DataGridTextColumn x:Name="FilterTextCol01"
IsReadOnly="False"
Width="{Binding ElementName=TextCol01, Path=ActualWidth, Mode=TwoWay}" />
</DataGrid.Columns>
Second:
<DataGridTextColumn CellStyle="{StaticResource DataGridColumnContentLeft}"
local:DataGridUtil.Name="TextCol01"
x:Name="TextCol01"
Header="TextCol01"
SortMemberPath="TextCol01"
Binding="{Binding TextCol01}"
Width="Auto"
IsReadOnly="True"/>
Binding of the width of first column to the width of the second doesn’t work.
If I’m doing it in code that way:
FilterTextCol01.Width = TextCol01.ActualWidth;
It works.
Could anyone tell me why the first approach doesn’t work?
Because
DataGridcolumns are abstract objects which do not appear in the logical or visual tree of your window. You cannot bind properties on them usingElementName(there will be no namescope which is needed for those bindings).You can try using
Sourceandx:Referenceinstead, e.g.