如何使用相互依赖的值创建属性?

时间:2016-11-23 23:24:50

标签: c# wpf data-binding properties inotifypropertychanged

在WPF中,我想为属性创建三个文本框:FullName,FirstName和LastName。这些文本框的文本将基于左侧的列表框(如下面的程序图所示)。我已经完成了从列表框到文本框的文本,但我想同步名称的值,以便:

更改FullName的值,更改列表框,FirstName和LastName框

并更改FirstName和LastName的值会更改FullName和listbox

这是程序的样子: (Program image)

好的,多亏了Jared,我能够通过对他的答案做一些小改动来解决问题,但我不喜欢我的解决方案大声笑,有人可以建议对这个程序进行更好的修改吗?

    class Student : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            FirstName = value.Split(" ".ToCharArray())?[0];
            LastName = value.Split(" ".ToCharArray())?[1];

            OnPropertyChanged("Name");
            OnPropertyChanged("FirstName");
            OnPropertyChanged("LastName");
        }
    }

    private string firstName;
    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
            name = value + " " + LastName;
            OnPropertyChanged("FirstName");
            OnPropertyChanged("Name");
        }
    }

    private string lastName;
    public string LastName
    {
        get
        {
            return lastName;
        }
        set
        {
            lastName = value;
            name = FirstName + " " + value;
            OnPropertyChanged("LastName");
            OnPropertyChanged("Name");
        }
    }

    private Course _course;

    public Course Course
    {
        get { return _course; }
        set
        {
            _course = value;
            OnPropertyChanged("Course");
        }
    }


    private int _age;

    public int Age
    {
        get { return _age; }
        set
        {
            _age = value;
            OnPropertyChanged("Age");
        }
    }
    private DateTime _birthday;

    public DateTime Birthday
    {
        get { return _birthday; }
        set
        {
            _birthday = value;
            OnPropertyChanged("Birthday");
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

1 个答案:

答案 0 :(得分:0)

我相信你需要的是一个如下的课程。然后,您将列表框绑定到这些列表框的集合,并将表单元素绑定到当前所选项目。

我在名字和姓氏之间添加了一个空格。如果你想删除它,你需要找到另一种分割名字和姓氏的方法,比如检查大小写。

由于无论修改了哪个属性,都会设置FullName,因此我将所有FirePropertyChanged调用放入其setter中。我还没有对此进行测试,但我认为它会起作用。 :)

编辑:更改了FullName setter以设置f​​irstName和lastName的私有变量,而不是public属性。

public class Student : INotifyPropertyChanged
{
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
        set
        {

            firstName = value.Split(" ".ToCharArray())?[0];
            lastName= value.Split(" ".ToCharArray())?[1];
            FirePropertyChanged("FullName");
            FirePropertyChanged("FirstName");
            FirePropertyChanged("LastName");
        }
    }

    private string firstName;
    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
            FullName = value + " " + LastName;
        }
    }

    private string lastName;
    public string LastName
    {
        get
        {
            return firstName;
        }
        set
        {
            lastName = value;
            FullName = FirstName + " " + value;
        }
    }

    public void FirePropertyChanged (string PropertyName)
    {
        PropertyChanged.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}