依赖项属性绑定未更新

Rem*_*tec 2 .net c# wpf xaml dependency-properties

我已经定义了一个依赖项属性如下:

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); }
}
Run Code Online (Sandbox Code Playgroud)

当我的应用程序启动时,我这样做....

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

...应将值初始化为其起始值 - 在本例中为400.

然后我在我的UI中将一列网格绑定到此属性...

<ColumnDefinition 
    Name="ProductInfo" 
    Width="{Binding Path=AnimateColumnWidth,
                    Converter={StaticResource doubleToGridLength},
                    Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

据我了解,由于列宽已绑定到此属性,每当我更新属性时,列宽也应更新.

当我更改属性时,宽度不会更新,我做错了什么?我也试图通过动画更新它,也不起作用.此外,在AnimateColumnWidth属性的getter上设置的断点永远不会被命中 - 这意味着没有任何东西试图检索该属性.

(这确实很明显,我已经在某处破坏了某些东西!!)

脚注:

转换后的值是在我的应用程序的根命名空间中定义的(我相信如果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;
    }
}
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 5

您注册属性为"AnimateColumnWidthProperty""错误",只有字段名称是任意的,您可能想要"AnimateColumnWidth"(或者您更改绑定当然,但是因为路径指向未注册的属性,它会失败).

您可能还想阅读有关调试绑定的内容,然后您可以发现这些错误,因为它们将由绑定引擎报告.(类似于"在对象y上找不到属性x").

也使用在getter方法或setter方法断点不会告诉你任何东西,绑定引擎并没有使用它们,他们只是为了自己方便.