如何在MVM模式中的WPF用户控件中绑定属性 [英] how to bind properties in wpf usercontrol in mvvm pattern

查看:142
本文介绍了如何在MVM模式中的WPF用户控件中绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在棱镜框架的mvvm模型中开发一个wpf应用程序.我在一个模块中创建了一个用户控件,并在另一个模块中使用了.我成功地将该用户控件导入到我的模块中,但是我无法将属性绑定到该用户控件.它充当单个控件,而不是用户控件中的不同控件.所以我要做什么.以下是我的代码

我的用户控件包含
3个单选按钮
3个文字框
1个数据网格

然后我将此用户控件引用到我的项目中:

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


解决方案

如果要使用绑定,则需要将用户控件公开为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.


这篇关于如何在MVM模式中的WPF用户控件中绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆