WPF 绑定到网格列宽 [英] WPF Binding to Grid Column Width
问题描述
我正在尝试将我的一个用户控件中的 DependancyProperty
绑定到 Grid 中
Column
的 Width
属性代码>.
I'm attempting to bind a DependancyProperty
in one of my usercontrols to the Width
property of a Column
in a Grid
.
我有类似这样的代码:
<Grid x:Name="MyGridName">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="TitleSection" Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>...</Grid.RowDefinitions>
<GridSplitter x:Name="MyGridSplitter" Grid.Row="0" Grid.Column="0" ... />
</Grid>
在单独的用户控件中,我定义了以下DependancyProperty
.
In a separate Usercontrol I have the followingDependancyProperty
defined.
public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(int), typeof(MyUserControl));
public int TitleWidth
{
get { return (int)base.GetValue(TitleWidthProperty); }
set { base.SetValue(TitleWidthProperty, value); }
}
我正在代码中创建用户控件的实例,因此我有一个类似于此的绑定语句:
I am creating instances of the Usercontrol in code, hence I have a binding statement similar to this :
MyUserControl Cntrl = new MyUserControl(/* Construction Params */);
BindingOperations.SetBinding(Cntrl , MyUserControl.AnotherProperty, new Binding { ElementName = "objZoomSlider", Path = new PropertyPath("Value"), Mode = BindingMode.OneWay });
BindingOperations.SetBinding(Cntrl , MyUserControl.TitleWidthProperty, new Binding { ElementName = "TitleSection", Path = new PropertyPath("ActualWidth"), Mode = BindingMode.OneWay });
/* Other operations on Cntrl */
定义的第一个绑定非常有效,虽然它绑定到实际的 UIElement(在本例中为 Slider),但绑定到TitleSection"(即 Grid 中定义的 ColumnDefinition)失败.在代码中放置断点并监视TitleSection"会返回预期的对象.
The first binding defined works fantastically, although that is binding to an actual UIElement (in this case a Slider), but the Binding to "TitleSection" (which is the ColumnDefinition defined in the Grid) fails. Putting a breakpoint in the code and doing a watch on "TitleSection" returns the expected object.
我开始怀疑不能绑定 x:Name'd ColumnDefinition.谁能建议我如何才能绑定到网格中第一列的不断变化的宽度?
I am beginning to suspect that a x:Name'd ColumnDefinition can't be bound to. Can anyone suggest how I might be able to bind to the changing width of the first column in my grid?
编辑 #1 - 回答评论
数据绑定失败"是因为在 TitleWidth
属性的设置器上设置了断点,并使用 GridSplitter 控件调整第一列的大小,断点永远不会被命中.此外,当 DependancyProperty TitleWidth
更改未执行时,我希望触发的代码.
The databinding 'fails' in the sense that with a breakpoint set on the setter for the TitleWidth
property, and using the GridSplitter control to resize the first column, the breakpoint is never hit. Additionally, code I would expect to be fired when the DependancyProperty TitleWidth
changes does not get executed.
正在创建用户控件并将其添加到 Window_Loaded
函数中的 Grid 内的 Stackpanel.我希望在构建用户控件时已经呈现了网格.当然,x:Name'd 元素 TitleSection
是可观察的,并且在构造它们时/在绑定发生之前其值为 100
.
The usercontrol is being created and added to a Stackpanel within the Grid in the Window_Loaded
function. I would expect that the Grid has been rendered by the time the Usercontrols are being constructed. Certainly the x:Name'd Element TitleSection
is watchable and has a value of 100
when they are being constructed / before the binding is happening.
编辑 #2 - 可能与此有关?
我一直在浏览 MSDN 页面中的 Grid ColumnDefinition 文档,并且遇到了 GridLength() 但我不知道如何在绑定表达式中使用它.我不能在绑定代码中使用关联的 GridLengthConverter 作为转换器,因为它不是从 IValueConverter 派生的.
I've been having a sniff round the MSDN pages for the Grid ColumnDefinition documentation and have come across GridLength() but I can't get my head around how I can use this in a binding expression. I cannot use the associated GridLengthConverter as a converter in the binding code as it does not derive from IValueConverter.
我倾向于以某种方式绑定到 Grid 对象中一个单元格的 ActualWidth 属性.它似乎不像绑定到列定义那样干净,但目前我无法让它工作.
I am leaning towards somehow binding to the ActualWidth property of one of the cells in the Grid object. It doesn't seem as clean as binding to the column definition, but at the moment I cannot get that to work.
推荐答案
好吧,我有一些工作要做,我会为后代解释一下:
Well I have got a bit of a kludge working, I'll explain how for future generations:
基本上我有一个 2 列、多行网格,在第一列中有一个右对齐的拆分器,因此如果它包含的内容需要更多空间,用户可以调整它的大小.为了使事情复杂化,我有一个用户控件以编程方式加载到一些行中,这些行的 columnSpan 为 2 用于呈现目的(内容从一个单元格渗入"到下一个单元格).
Essentially I have a 2 column, multi row grid with a splitter right aligned in the first column so it can be resized by the user if the content it contains requires more space. To complicate things I have a user control being loaded programatically into some of the rows which has a columnSpan of 2 for rendering purposes (content 'bleeds' from one cell into the next).
当调整第一列的大小时,我需要将其反映在用户控件中.首先,我尝试绑定到 ColumnDefinition,但它确实没有发挥作用.
When the first column is resized I need this to be reflected in the usercontrol. Firstly I tried binding to the ColumnDefinition but it really wasn't playing ball.
我是如何修复/组装它的
在第一列的备用单元格中,我添加了一个带有 x:Name 的 <Label>
以使其可访问.因为它在一个单元格中,所以它具有拉伸"的默认属性并完全填充单元格.随着使用拆分器调整列的大小,它会调整大小.绑定到标签的 ActualWidth
属性意味着对列大小的更改将正确传达给我的 columnSpanned 用户控件中的 DependancyProperty.
In a spare cell in the first column I added a <Label>
with an x:Name to make it accessible. As it is in a cell it has default properties of 'Stretch' and fills the cell completely. It gets resized as the column is resized using the splitter. Binding to the Label's ActualWidth
property means that changes to the size of the column are communicated to the DependancyProperty in my columnSpanned usercontrol correctly.
想法
显然,尽管 ColumnDefinition 在更改时具有 ActualWidth
属性,但它似乎不会在内部触发 PropertyChanged
事件(或者这是我的最佳猜测).这可能是一个错误,也可能是设计使然,但对我来说,这意味着我不得不使用不太干净的解决方案.
Obviously, despite ColumnDefinition having an ActualWidth
property when it changes it doesn't appear to fire the PropertyChanged
event internally (or thats my best guess). This may be a bug, or by design, but for me it means I've had to use a less clean solution.
这篇关于WPF 绑定到网格列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!