依赖属性绑定未更新

发布于 2024-12-12 14:23:07 字数 1652 浏览 0 评论 0原文

我定义了一个依赖属性,如下所示:

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); }
}

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

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

这应该将值初始化为其起始值 - 在本例中为 400。

然后我在我的 UI 中绑定了网格的列到这个属性...

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

据我了解,由于列宽绑定到这个属性,每当我更新属性时,列宽也应该更新。

当我更改属性时宽度没有更新,我做错了什么?我还尝试通过动画来更新它,但这也不起作用。此外,在 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;
    }
}

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.

I have then bound a column of a grid in my UI to this property...

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

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

[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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-12-19 14:23:09

我没有做的一件事是将我想要影响的列的网格的数据上下文设置为“this”。

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

One thing I hadnt done was set the datacontext of the grid who's column I wanted to affect to "this".

public MainWindow()
{
    InitializeComponent();
    ProductsArea.DataContext = this;
}
听风吹 2024-12-19 14:23:08

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

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

另外,在 getter 或 setter 中使用断点不会告诉您任何信息,绑定引擎不会使用它们,它们只是为了您自己的方便。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文