我试图通过手动绑定Width
的{{1}}属性来解决我以前的question,这是我的XAML代码的第一个版本。
DataGridTextColumn
经过一番研究后,我发现这个post似乎提供了我的问题的答案,并更新了我的 <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
但现在我得到了这个XamlParseException
<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>
那么如何将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'.
的{{1}}属性绑定到其父Width
的{{1}}属性
答案 0 :(得分:3)
有同样的问题并发现使用x:Reference你不能引用你正在使用它的任何容器。
令人讨厌的黑客,但我想如果你创建了一些其他控件(TextBlock
)并将其宽度绑定到DataGrid
ActualWidth
,然后使用x:参考TextBlock
它会避免周期性参考
<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>