WPF 依赖属性不起作用

发布于 2024-10-05 23:16:51 字数 620 浏览 1 评论 0原文

我有一个自定义依赖属性,如下定义:

public static readonly DependencyProperty MyDependencyProperty =
DependencyProperty.Register(
"MyCustomProperty", typeof(string), typeof(MyClass));

    private string _myProperty;
    public string MyCustomProperty
    {
        get { return (string)GetValue(MyDependencyProperty); }
        set
        {
            SetValue(MyDependencyProperty, value);
        }
    }

现在我尝试在 XAML 中设置该属性,

<controls:TargetCatalogControl MyCustomProperty="Boo" />

但是 DependencyObject 中的 setter 永远不会被命中!尽管当我将属性更改为常规属性而不是 Dep Prop 时确实如此

I have a custom Dependency Property defined like so:

public static readonly DependencyProperty MyDependencyProperty =
DependencyProperty.Register(
"MyCustomProperty", typeof(string), typeof(MyClass));

    private string _myProperty;
    public string MyCustomProperty
    {
        get { return (string)GetValue(MyDependencyProperty); }
        set
        {
            SetValue(MyDependencyProperty, value);
        }
    }

Now I try set that property in XAML

<controls:TargetCatalogControl MyCustomProperty="Boo" />

But the setter in DependencyObject never gets hit! Although it does when I change the property to be a regular property and not a Dep Prop

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

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

发布评论

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

评论(3

断肠人 2024-10-12 23:16:51

试试这个..

    public string MyCustomProperty
    {
        get 
        { 
            return (string)GetValue(MyCustomPropertyProperty); 
        }
        set 
        { 
            SetValue(MyCustomPropertyProperty, value); 
        }
    }

    // Using a DependencyProperty as the backing store for MyCustomProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyCustomPropertyProperty =
        DependencyProperty.Register("MyCustomProperty", typeof(string), typeof(TargetCatalogControl), new UIPropertyMetadata(MyPropertyChangedHandler));


    public static void MyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // Get instance of current control from sender
        // and property value from e.NewValue

        // Set public property on TaregtCatalogControl, e.g.
        ((TargetCatalogControl)sender).LabelText = e.NewValue.ToString();
    }

    // Example public property of control
    public string LabelText
    {
        get { return label1.Content.ToString(); }
        set { label1.Content = value; }
    }

Try this..

    public string MyCustomProperty
    {
        get 
        { 
            return (string)GetValue(MyCustomPropertyProperty); 
        }
        set 
        { 
            SetValue(MyCustomPropertyProperty, value); 
        }
    }

    // Using a DependencyProperty as the backing store for MyCustomProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyCustomPropertyProperty =
        DependencyProperty.Register("MyCustomProperty", typeof(string), typeof(TargetCatalogControl), new UIPropertyMetadata(MyPropertyChangedHandler));


    public static void MyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // Get instance of current control from sender
        // and property value from e.NewValue

        // Set public property on TaregtCatalogControl, e.g.
        ((TargetCatalogControl)sender).LabelText = e.NewValue.ToString();
    }

    // Example public property of control
    public string LabelText
    {
        get { return label1.Content.ToString(); }
        set { label1.Content = value; }
    }
相思碎 2024-10-12 23:16:51

除非您手动调用它,否则不会。您可以将一个属性更改处理程序添加到 DependancyProperty 构造函数调用中,以便在属性更改时收到通知。

调用此构造函数:

http://msdn.microsoft.com/en-us/library /ms597502.aspx

使用此构造函数创建的 PropertyMetadata 实例:

http: //msdn.microsoft.com/en-us/library/ms557327.aspx

编辑:此外,您没有正确实现依赖属性。您的 getset 应分别使用 GetValueSetValue,并且您不应该有类成员来存储价值。 DP 的成员名称也应该是 {PropertyName}Property,例如 MyCustomPropertyProperty 如果 get/set 和注册的属性名称是MyCustomProperty。请参阅 http://msdn.microsoft.com/en-us/library/ms753358。 aspx 了解更多信息。

希望有帮助。

It doesn't, unless you call it manually. There's a property-changed handler you can add to the DependancyProperty constructor call to be notified of when the property changes.

Call this constructor:

http://msdn.microsoft.com/en-us/library/ms597502.aspx

With a PropertyMetadata instance created by this constructor:

http://msdn.microsoft.com/en-us/library/ms557327.aspx

EDIT: Also, you are not implementing the dependancy property correctly. Your get and set should use GetValue and SetValue respectively, and you should not have a class member to store the value. The member name of the DP should also be {PropertyName}Property, e.g. MyCustomPropertyProperty if the get/set and property name as registered is MyCustomProperty. See http://msdn.microsoft.com/en-us/library/ms753358.aspx for more information.

Hope that helps.

时光沙漏 2024-10-12 23:16:51

也许您正在使用 MVVM,并覆盖 View 的 DataContext ?

如果这样做,则更改 MyCustomProperty 的事件将在原始 DataContext 上引发,而不是在新 ViewModel 上引发。

Maybe you are using MVVM, and overriding the DataContext of your View ?

If you do, then the event for changing MyCustomProperty will be raised on the original DataContext and not on the new ViewModel.

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