简体   繁体   English

依赖项属性绑定未更新

[英]Dependency Property binding not being updated

I have defined a dependency propery as follows: 我已经定义了一个依赖属性,如下所示:

public static readonly DependencyProperty AnimateColumnWidthProperty =
    DependencyProperty.Register("AnimateColumnWidthProperty", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0));

public double AnimateColumnWidth
{
    get { return (double)GetValue(AnimateColumnWidthProperty); }
    set { SetValue(AnimateColumnWidthProperty, value); }
}

When my application starts I do this.... 当我的应用程序启动时,我将执行此操作。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    AnimateColumnWidth = Properties.Settings.Default.ProductInfoWidthExpanded;
}

... which should initialise the value to its starting value - in this case 400. ...应将值初始化为其初始值-在这种情况下为400。

I have then bound a column of a grid in my UI to this property... 然后,我将UI中的网格列绑定到此属性...

<ColumnDefinition 
    Name="ProductInfo" 
    Width="{Binding Path=AnimateColumnWidth,
                    Converter={StaticResource doubleToGridLength},
                    Mode=TwoWay}" />

As I understand it, as the column width is bound to this property, whenever I update the property the column width should also update. 据我了解,由于列宽绑定到此属性,所以每当我更新该属性时,列宽也应更新。

What am I doing wrong as the width does not update when I change the property? 更改属性后宽度没有更新,这是我做错了什么? I am also trying to update it via an animation which also doesnt work. 我也在尝试通过动画也无法更新它。 Additionally, a breakpoint set on the getter of the AnimateColumnWidth property is never hit - meaning that nothing ever tried to retrieve the property. 此外,永远不会命中在AnimateColumnWidth属性的getter上设置的断点-这意味着从未尝试过检索该属性。

(This did work so clearly I have broken something somewhere!!) (这样做确实很有效,我在某个地方坏了!!)

Footnote: 脚注:

The value converted is defined in the root namespace of my app (I believe that WPF would complain if it couldnt find it). 转换后的值是在我的应用程序的根名称空间中定义的(我相信WPF如果找不到它会抱怨)。

[ValueConversion(typeof(Double), typeof(GridLength))]
public class DoubleToGridLength : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new GridLength((double)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((GridLength)value).Value;
    }
}

You register the property as "AnimateColumnWidthProperty" , which is "wrong", only the field name is arbitrary, you probably want "AnimateColumnWidth" (or you change the binding of course, but as is, it fails since the path points to an unregistered property). 您将属性注册为"AnimateColumnWidthProperty" ,这是“错误的”,只有字段名称是任意的,您可能想要"AnimateColumnWidth" (或者您当然更改了绑定,但是按原样,它失败了,因为路径指向未注册的属性)。

You might want to read something on debugging bindings as well, then you can spot such errors as they will be reported by the binding engine. 您可能还想阅读有关调试绑定的内容 ,然后可以发现此类错误,因为绑定引擎将报告这些错误。 (something like "property x not found on object y"). (类似于“在对象y上找不到属性x”)。

Also using breakpoints in the getters or setters does not tell you anything, the binding engine does not use them, they are just for your own convenience. 也使用在getter方法或setter方法断点不会告诉你任何东西,绑定引擎使用他们,他们只是为了自己方便。

One thing I hadnt done was set the datacontext of the grid who's column I wanted to affect to "this". 我没有做的一件事是将我想影响的列的网格的数据上下文设置为“ this”。

public MainWindow()
{
    InitializeComponent();
    ProductsArea.DataContext = this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM