none
属性绑定问题,路过的各位大师帮忙来解决下。 RRS feed

  • 问题

  • 一个文本控件绑定了一个类的属性,在类的设置属性里,赋值为一个固定常量。

    这样就不会触发界面刷新,但是属性值是固定常量,为什么WPF的绑定要加oldValue==newValue就不做处理?


    snyod

    2013年11月8日 10:32

答案

  • 你好,

    根据你的描述,我用以下的例子来解释:

    首先按照你说的:“一个文本控件绑定了一个类的属性,在类的设置属性里,赋值为一个固定常量”:

    public class Person
    {
            private string firstName = String.Empty;
    
            public Person()
            {
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "Franklin";
                    }
                }
            }
    }

    这种情况下,XAML虽然是绑定了,但是界面是无法显示数据的,必须是在Set中设置this.firstName = value; 实例化中再设置属性值才可以。

    public Person(){
                firstName = "Franklin";
    }


    如果你实现了INotifyPropertyChanged接口,如以下代码,虽然界面第一次不会出现Franklin,但是只要更新过属性值,界面便会立刻显示Franklin:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "Franklin";
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
    }

    以下代码是较好的带属性值更新的Person类:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
                firstName = "Franklin";
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = value;
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
    }

    #INotifyPropertyChanged Interface
    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月11日 5:52
    版主

全部回复

  • 你好,

    根据你的描述,我用以下的例子来解释:

    首先按照你说的:“一个文本控件绑定了一个类的属性,在类的设置属性里,赋值为一个固定常量”:

    public class Person
    {
            private string firstName = String.Empty;
    
            public Person()
            {
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "Franklin";
                    }
                }
            }
    }

    这种情况下,XAML虽然是绑定了,但是界面是无法显示数据的,必须是在Set中设置this.firstName = value; 实例化中再设置属性值才可以。

    public Person(){
                firstName = "Franklin";
    }


    如果你实现了INotifyPropertyChanged接口,如以下代码,虽然界面第一次不会出现Franklin,但是只要更新过属性值,界面便会立刻显示Franklin:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "Franklin";
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
    }

    以下代码是较好的带属性值更新的Person类:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
                firstName = "Franklin";
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = value;
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
    }

    #INotifyPropertyChanged Interface
    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月11日 5:52
    版主
  • hello,

    非常感谢你的回复,但是现在还有问题。

    public class Person : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
     
            public Person()
            {
                firstName = "Franklin";
            }
     
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "test";
                        PropertyChanged(thisnew PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
        }

    这样的话,界面上一直显示的都是我输入的值,不是我在后台设置的“test”值。


    snyod

    2013年11月30日 5:14
  • 为什么要设置属性的值为“test”,如果输入的是test@,需要把@去掉,然后就剩下"test"了,但是界面上还是显示的"test@"。

    snyod

    2013年11月30日 5:17
  • hello,

    非常感谢你的回复,但是现在还有问题。

    public class Person : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
     
            public Person()
            {
                firstName = "Franklin";
            }
     
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = "test";
                        PropertyChanged(thisnew PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
        }

    这样的话,界面上一直显示的都是我输入的值,不是我在后台设置的“test”值。


    snyod

    你好,

    对于你的代码,因为实例化是已经对 firstName 赋值为Franklin,所以不会调用Set,这样不会是你想要的test, 这种情况下,你也无法对新建的实例对象后台设置FirstName了,你必须用以下代码实现设置FirstName:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
                firstName = "test";
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = value ;
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
    }

    实例化:

    Person person = new Person();

    或者:

    public class Person : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
            private string firstName = String.Empty;
    
            public Person()
            {
                
            }
    
            public string FirstName
            {
                get
                {
                    return firstName;
                }
                set
                {
                    if (value != firstName)
                    {
                        this.firstName = value ;
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
            }
     }

     

    实例化时:

    Person person = new Person();
    person.FirstName="test";


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月2日 5:57
    版主