且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MVM模式中的WPF用户控件中绑定属性

更新时间:2022-10-21 11:12:01

如果要使用绑定,则需要将用户控件公开为DependencyProperty-可以访问属性在XAML或后面的代码中.
作为示例,您可以将单选按钮显示为

 公共 静态 只读 DependencyProperty Radio1Property = DependencyProperty.Register(" 类型(YourUserContol),
 PropertyMetadata());

        公共 RadioButton Radio1
        {
            获取 {返回(单选按钮). GetValue(Radio1Property); }
             set  { this  .SetValue(Radio1Property, value ); }
        } 





或者您可以将控件公开为CLR公共属性,然后可以在后面的代码中进行访问.

希望对您有所帮助.


hi
I am developing one wpf application in mvvm model in prism framework.i created one usercontrol in one module and i used in another module.i successfully import that usercontrol in my module but i can''t bind properties to that user control. it act as a single control not different control in user control.so what i will do .following is my code

my usercontrol contain
3 radio button
3 text box
1 data grid

then i reference this usercontrol to my project:

xmlns:l="clr-namespace:MagicHospital.Controls;assembly=MagicHospital.Controls"
       <l:PatientDetailsUserControl></l:PatientDetailsUserControl>


then how can i bind properties to controls in usercontrol.

If you want to use Binding you need to expose the usercontrol(s) as a DependencyProperty - Can access proprty in XAML or code behind.
AS an example you can expose the radiobutton as

public static readonly DependencyProperty Radio1Property = DependencyProperty.Register("Radio1", typeof(RadioButton),
typeof(YourUserContol),
new PropertyMetadata());

        public RadioButton Radio1
        {
            get { return (RadioButton)this.GetValue(Radio1Property); }
            set { this.SetValue(Radio1Property, value); }
        }





or you can expose the controls as CLR-Public properties and you can then access in your code behind.

I hope this helps.