I tried to solve my previous question with manually binding the Width
property of the DataGridTextColumn
here is the first Version of my XAML Code.
Had the same issue and found out that using x:Reference you cannot refer to any container of the object you are using it from.
Nasty hack, but I'd imagine if you created some other control (TextBlock
) and bound it's width to the DataGrid
ActualWidth
and THEN used x:Reference on that TextBlock
it would avoid the cyclical reference
<TextBlock x:Name="TextBlock1" Width="{Binding ElementName=myDataGrid, Path=ActualWidth}" />
<DataGrid AutoGenerateColumns="False" Background="White" ItemsSource="{Binding Items, Mode=OneWay}"
HorizontalGridLinesBrush="Silver" VerticalGridLinesBrush="Silver"
Margin="332,10,10,10" CanUserAddRows="False" CanUserDeleteRows="False"
x:Name="myDataGrid" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, Source={x:Reference Name=TextBlock1}}" IsReadOnly="True" Header="Column1" Binding="{Binding Value1, Mode=OneWay}" />
<DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, Source={x:Reference Name=TextBlock1}}" IsReadOnly="True" Header="Column2" Binding="{Binding Value2, Mode=OneWay}"/>
<DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, Source={x:Reference Name=TextBlock1}}" IsReadOnly="True" Header="Column3" Binding="{Binding Value3, Mode=OneWay}"/>
</DataGrid.Columns>
</DataGrid>