Binding the Width Property of a DataGridColumn to the ActualWidth of the parent DataGrid

有些话、适合烂在心里 提交于 2019-12-20 03:46:12

问题


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.

   <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}, RelativeSource={RelativeSource AncestorType=DataGrid}}" IsReadOnly="True" Header="Column1" Binding="{Binding Value1, Mode=OneWay}" />
            <DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, RelativeSource={RelativeSource AncestorType=DataGrid}}" IsReadOnly="True" Header="Column2" Binding="{Binding Value2, Mode=OneWay}"/>
            <DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, RelativeSource={RelativeSource AncestorType=DataGrid}}" IsReadOnly="True" Header="Column3" Binding="{Binding Value3, Mode=OneWay}"/>
        </DataGrid.Columns>
    </DataGrid>

After a little research i found this post that seems to provide the answer to my problem and i updated my DataGrid code.

   <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=myDataGrid}}" IsReadOnly="True" Header="Column1" Binding="{Binding Value1, Mode=OneWay}" />
            <DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, Source={x:Reference Name=myDataGrid}}" IsReadOnly="True" Header="Column2" Binding="{Binding Value2, Mode=OneWay}"/>
            <DataGridTextColumn Width="{Binding Path=ActualWidth, Converter={StaticResource ResourceKey=WidthValueConverter}, Source={x:Reference Name=myDataGrid}}" IsReadOnly="True" Header="Column3" Binding="{Binding Value3, Mode=OneWay}"/>
        </DataGrid.Columns>
    </DataGrid>

but now im getting this XamlParseException

Cannot call MarkupExtension.ProvideValue because of a cyclical dependency. Properties inside a 
MarkupExtension cannot reference objects that reference the result of the MarkupExtension. 
The affected MarkupExtensions are:
'System.Windows.Data.Binding' Line number '37' and line position '37'.
'System.Windows.Data.Binding' Line number '38' and line position '37'.
'System.Windows.Data.Binding' Line number '39' and line position '37'.

So how can i bind the Width property of a DataGridColumn to the ActualWidth property of its parent DataGrid


回答1:


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>


来源:https://stackoverflow.com/questions/12871551/binding-the-width-property-of-a-datagridcolumn-to-the-actualwidth-of-the-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!