首页 新闻 会员 周边

WPF数据绑定

0
悬赏园豆:50 [已解决问题] 解决于 2013-07-31 19:26

我将MainViewModel设置成MainWindow的DataContext,这个时候修改用户名代表的TextBox(txtBox1),此时不会调用MainViewModel.cs中的Set方法

 1 private User userinfo;
 2 
 3         /// <summary>
 4         /// Sets and gets the UserInfo property.
 5         /// Changes to that property's value raise the PropertyChanged event. 
 6         /// </summary>
 7         public User UserInfo
 8         {
 9             get
10             {
11                 return userinfo;
12             }
13 
14             set
15             {
16                 
17                 if (userinfo == value)
18                 {
19                     return;
20                 }
21                 userinfo = value;
22                 RaisePropertyChanged("UserInfo");
23             }
24         }

而是调用User.cs中的Set方法:

public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Password { get; set; }
    }

 

此时如果我再界面上再放一个TextBox(txtBox2),txtBox1和txtBox2都绑定到UserInfo.Name上,

<TextBox Grid.Column="1" Grid.Row="0">
            <TextBox.Text>
                <Binding Path="UserInfo.Name" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay">
                <Binding.ValidationRules>
                    <!--<ExceptionValidationRule />-->
                    <local:UserValidate ValidateType="UserName" ></local:UserValidate>
                </Binding.ValidationRules>
            </Binding>
            </TextBox.Text>
        </TextBox>

此时,修改txtBox1,txtBox2的值也会更改,但是修改txtBox1是没有调用RaisePropertyChanged方法呀,调用的是User这个实体里Name属性的Set方法,User这个类也没有实现INotifyPropertyChanged接口,然而txtBox2为什么会跟着txtBox1修改呢?

 

 

如果我在ViewModel中加一个成员:

private string myname;

        /// <summary>
        /// Sets and gets the MyName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string MyName
        {
            get
            {
                return myname;
            }

            set
            {
                if (myname == value)
                {
                    return;
                }
                myname = value;
                RaisePropertyChanged("MyName");
            }
        }

txtBox1和txtBox2都与这个MyName绑定,此时txtBox1更改因为调用了MyName的Set方法,所以就会调用RaisePropertyChanged方法,txtBox2也就会更改了,这个倒是好理解,但是前面改txtBox1时并没有调用RaisePropertyChanged方法,txtBox2却也跟着改变了。

 

这是为什么,请各位大侠指教。。。

WPF
水秀山青的主页 水秀山青 | 初学一级 | 园豆:157
提问于:2013-07-31 14:51
< >
分享
最佳答案
0

控件判定实体对象中的属性时,它会对主动注册实体对象的OnPropertyChanged事件,前提是 自己去改变实体对象的;如果是其它情况改变的,则也要手动注册实体对象的OnPropertyChanged事件.

收获园豆:50
Yu | 专家六级 |园豆:12980 | 2013-07-31 16:25

MainViewModel.cs部分代码:

private User userinfo;

        /// <summary>
        /// Sets and gets the UserInfo property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public User UserInfo
        {
            get
            {
                return userinfo;
            }

            set
            {
                
                if (userinfo == value)
                {
                    return;
                }
                userinfo = value;
            }
        }

User.cs

public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Password { get; set; }
    }

 

你的意思是当我将UserInfo.Name绑定到控件上时,会自动给MainViewModel.cs中UserInfo的Set方法,还有User.cs里面的每个Set方法注册OnPropertyChanged事件,当然这只有在界面上手动更改TextBox的值时有效,如果在后台用一个Button的Click事件改的话就必须要手动注册OnPropertyChanged事件了?

水秀山青 | 园豆:157 (初学一级) | 2013-07-31 17:15

@水秀山青: 是的

Yu | 园豆:12980 (专家六级) | 2013-07-31 19:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册