WPF usercontrol Twoway binding Dependency Property

后端 未结 2 1571
既然无缘
既然无缘 2020-12-05 19:33

I created a Dependency Property in the usercontrol, but however changes in the usercontrol was NOT notified to the Viewmodel

Usercontrol



        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 19:44

    Bind the TextBox.Text property in the UserControl to its SampleProperty like this:

    
    

    Now you could simply remove your OnSamplePropertyChanged callback.


    You might also register SampleProperty to bind two-way by default like this:

    public static readonly DependencyProperty
        SamplePropertyProperty = DependencyProperty.Register(
            "SampleProperty", typeof(string), typeof(UserControl1),
            new FrameworkPropertyMetadata(
                null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    

提交回复
热议问题