Dependency Property assigned with value binding does not work

后端 未结 2 1632
暗喜
暗喜 2020-12-30 07:09

I have a usercontrol with a dependency property.

public sealed partial class PenMenu : UserControl, INotifyPropertyChanged
{
    public event PropertyChanged         


        
2条回答
  •  感情败类
    2020-12-30 07:42

    It's frustrating isn't it? First, include a changed event handler. Like this:

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), 
        typeof(MyControl), new PropertyMetadata(string.Empty, Changed));
    private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var c = d as MyControl;
        // now, do something
    }
    

    Then, please read this article so you see there are more gotchas than just that one: http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html

    Best of luck!

提交回复
热议问题