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

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

问题描述

这似乎有点奇怪。
我有两个数据网格,每个一列。
第一:

 < D​​ataGrid.Columns> 
< D​​ataGridTextColumn
X:NAME =FilterTextCol01
WIDTH ={绑定的ElementName = TextCol01,路径= ActualWidth的,模式=双向}
IsReadOnly =FALSE/> ;
< /DataGrid.Columns>



二:

 < D​​ataGridTextColumn 
CellStyle ={StaticResource的DataGridColumnContentLeft}
当地时间:DataGridUtil.Name =TextCol01
X:NAME =TextCol01
头= TextCol01
SortMemberPath =TextCol01
绑定={结合TextCol01}
WIDTH =自动
IsReadOnly =真/>



第一列的宽度与第二宽度的结合不工作。
。如果我做它的代码方式:

  FilterTextCol01.Width = TextCol01.ActualWidth; 



它的工作原理。
谁能告诉我,为什么第一种方法不起作用?
谢谢!


解决方案

由于的DataGrid 列是抽象对象该做的的出现在窗口的逻辑或视觉树。用他们不能绑定属性的ElementName (将没有名称范围这是需要那些绑定)。



您可以尝试使用来源 X:参考 代替,例如:



<预类=郎咸平的XML prettyprint-覆盖> {绑定源= {X:参考TextCol01},路径= ActualWidth的}


it seems to be a bit odd. I have two datagrids with one column each. First:

<DataGrid.Columns>
   <DataGridTextColumn 
     x:Name="FilterTextCol01"
     Width="{Binding ElementName=TextCol01, Path=ActualWidth, Mode=TwoWay}" 
     IsReadOnly="False"/>
</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? Thanks!

解决方案

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天全站免登陆