绑定的DataGridColumn的Width属性到父DataGrid的ActualWidth的 [英] Binding the Width Property of a DataGridColumn to the ActualWidth of the parent DataGrid

查看:662
本文介绍了绑定的DataGridColumn的Width属性到父DataGrid的ActualWidth的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决我的previous <一个href=\"http://stackoverflow.com/questions/12758886/datagrid-columnwidth-property-ignored-in-datatemplate-for-row-details\">question与手工结合的宽度属性 DataGridTextColumn 这是我的XAML code的第一个版本。

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>

在进行一些研究之后,我发现这个帖子似乎提供答案,我的问题我更新了我的的DataGrid code。

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>

但我现在得到这个XamlParseException

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

所以,我怎么可以绑定一个的DataGridColumn ActualWidth的<中的宽度属性/ code>其父财产的DataGrid

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

推荐答案

有同样的问题,并发现使用X:参考您不能引用您正在使用它从对象的任何容器。
讨厌的黑客,但我想,如果你创建了一些其他控制(的TextBlock ),并约束它的宽度与 DataGrid的想象 ActualWidth的,然后利用X:参考上的TextBlock 这将避免循环参考值

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>

这篇关于绑定的DataGridColumn的Width属性到父DataGrid的ActualWidth的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆