绑定datagrid列宽 [英] Binding datagrid column width

查看:161
本文介绍了绑定datagrid列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个datagrids,每列一列。
首先:

 < DataGrid.Columns> 
< DataGridTextColumn x:Name =FilterTextCol01
IsReadOnly =False
宽度={Binding ElementName = TextCol01,Path = ActualWidth,Mode = TwoWay}/>
< /DataGrid.Columns>

第二:

 code>< DataGridTextColumn CellStyle ={StaticResource DataGridColumnContentLeft}
local:DataGridUtil.Name =TextCol01
x:Name =TextCol01
标题=TextCol01
SortMemberPath =TextCol01
Binding ={Binding TextCol01}
宽度=自动
IsReadOnly =True/>

将第一列的宽度与第二列的宽度绑定不起作用。
如果我在代码中这样做:

  FilterTextCol01.Width = TextCol01.ActualWidth; 

它可以工作。
任何人都可以告诉我为什么第一种方法不起作用?

解决方案

因为 DataGrid 列是在窗口的逻辑或视觉树中出现的不是的抽象对象。您不能使用 ElementName (不需要这些绑定所需的名称镜)来绑定属性。



您可以尝试使用 Source x:参考 ,例如

  { Binding Source = {x:Reference TextCol01},Path = ActualWidth} 


I have two datagrids with one column each. First:

<DataGrid.Columns>
   <DataGridTextColumn x:Name="FilterTextCol01" 
                       IsReadOnly="False" 
                       Width="{Binding ElementName=TextCol01, Path=ActualWidth, Mode=TwoWay}" />
</DataGrid.Columns>

Second:

<DataGridTextColumn CellStyle="{StaticResource DataGridColumnContentLeft}"
                    local:DataGridUtil.Name="TextCol01"
                    x:Name="TextCol01"
                    Header="TextCol01"
                    SortMemberPath="TextCol01"
                    Binding="{Binding TextCol01}" 
                    Width="Auto" 
                    IsReadOnly="True"/>

Binding of the width of first column to the width of the second doesn't work. If I'm doing it in code that way:

FilterTextCol01.Width = TextCol01.ActualWidth;

It works. Could anyone tell me why the first approach doesn't work?

解决方案

Because DataGrid columns are abstract objects which do not appear in the logical or visual tree of your window. You cannot bind properties on them using ElementName (there will be no namescope which is needed for those bindings).

You can try using Source and x:Reference instead, e.g.

{Binding Source={x:Reference TextCol01}, Path=ActualWidth}

这篇关于绑定datagrid列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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