WPF绑定到网格列宽 [英] WPF Binding to Grid Column Width

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

问题描述

我试图将我的一个usercontrols中的 DependancyProperty 绑定到 Width 属性的一个 Column Grid 中。



我的代码与此类似:

 < Grid x :名称= MyGridName > 
< Grid.ColumnDefinitions>
< ColumnDefinition x:Name =TitleSectionWidth =100/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>

< Grid.RowDefinitions> ...< /Grid.RowDefinitions>

< GridSplitter x:Name =MyGridSplitterGrid.Row =0Grid.Column =0... />
< / Grid>

在单独的Usercontrol中,我有以下 DependancyProperty 定义。


$ b $ pre $ 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); }
}

我在代码中创建Usercontrol的实例,因此我有一个绑定类似于这样的语句:

  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});
/ * Cntrl上的其他操作* /

定义的第一个绑定非常有效,绑定到一个实际的UIElement(在这个例子中是一个Slider),但是绑定到TitleSection(这是Grid中定义的ColumnDefinition)失败。在代码中放置一个断点并在TitleSection上执行监视可返回预期的对象。

我开始怀疑x:Name'd ColumnDefinition不能绑定到。 任何人都可以建议我如何绑定到网格中第一列的宽度变化?
$ b 编辑#1 - 回答评论



数据绑定'失败'的意思是,在setter上为 TitleWidth 属性,并使用GridSplitter控件调整第一列的大小,断点不会被击中。另外,当DependancyProperty TitleWidth 变化未得到执行时,我预计会触发代码。



usercontrol是在 Window_Loaded 函数中创建并添加到网格中的Stackpanel中。我期望在Usercontrols被构建的时候Grid已经被渲染了。当然,x:Name'd元素 TitleSection 是可见的,并且在构建时/之前具有 100 的值绑定正在发生。



编辑#2 - 可能与此有关?



我一直在为Grid GridDefinition文档的MSDN页面进行嗅探,并且遇到了 GridLength(),但我无法理解我如何在绑定表达式中使用它。我不能在绑定代码中使用关联的GridLengthConverter作为转换器,因为它不是从IValueConverter派生的。

我倾向于以某种方式绑定到Grid对象中某个单元格的ActualWidth属性。它看起来不像绑定到列定义那样干净,但目前我无法让它工作。 解决方案

嗯,我已经有了一些kludge工作,我会解释如何为未来世代:

基本上我有一个2列,多行网格与右对齐的分离器在第一列中,如果它包含的内容需要更多的空间,则用户可以调整它的大小。为了使事情复杂化,我将一个用户控件以编程方式加载到一些行中,其中columnSpan为2,用于呈现目的(内容从一个单元格到下一个单元格出血)。

当第一列调整大小时,我需要在usercontrol中反映这一点。首先,我尝试绑定到ColumnDefinition,但它确实不是在玩球。



我如何修复/损坏它



在第一列的备用单元中,我添加了一个带有x:Name的< Label> 以使其可访问。它在单元格中具有拉伸的默认属性并完全填充单元格。当列使用分离器调整大小时,它会被重新调整大小。绑定到标签的 ActualWidth 属性意味着对列大小的更改将正确传递到column spanned usercontrol中的DependancyProperty。



想法

显然,尽管ColumnDefinition具有 ActualWidth 属性改变它似乎并没有在内部激发 PropertyChanged 事件(或者这是我最好的猜测)。这可能是一个错误,也可能是设计,但对我来说,这意味着我必须使用一个不太干净的解决方案。


I'm attempting to bind a DependancyProperty in one of my usercontrols to the Width property of a Column in a Grid.

I have code similar to this:

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

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 */

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.

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?

EDIT #1 - To answer comments

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.

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.

EDIT #2 - Possibly something to do with this?

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.

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:

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

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.

How I fixed/Kludged it

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.

Thoughts

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屋!

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