2

我试图通过手动绑定这里的属性来解决我之前的问题,这是我的 XAML 代码的第一个版本。WidthDataGridTextColumn

   <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>

经过一番研究,我发现这篇文章似乎为我的问题提供了答案,我更新了我的DataGrid代码。

   <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>

但现在我得到了这个 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'.

那么如何将 a 的Width属性绑定到其父级DataGridColumn的属性ActualWidthDataGrid

4

1 回答 1

3

有同样的问题,并发现使用 x:Reference 您不能引用您正在使用它的对象的任何容器。讨厌的黑客,但我想如果你创建了一些其他控件(TextBlock)并将它的宽度绑定到DataGrid ActualWidth然后使用 x:ReferenceTextBlock它将避免循环引用

<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>
于 2012-11-06T18:58:12.567 回答