c#-将用户输入从文本框绑定到对象集合,并通过MVVM中的组合框填充

我对WPF和MVVM还是比较陌生,但是到目前为止,我找不到这种双向绑定方案的方法.我还应该提到,这不是一个学校项目,而是我自己对学习MVVM各个方面的兴趣,这是我自己的利益.

我的视图包含一个带有文本框的窗口,用于收集用户输入(学校名称,学校地址,学校电话等).视图还具有一个添加按钮,该按钮应保存来自文本框的输入,创建一个School对象,然后填充一个ComboBox(带有学校名称).因此,每当用户单击“添加”按钮时,新学校就会出现在“组合框”列表中,并且所有文本框都将重置并为新学校输入做好准备.我希望保留的另一个条件是,可以将TextBoxes与ComboBox中的选定项进行双向绑定.因此,当您从列表中选择一所学校时,将使用其对象的信息填充文本框,并可以对其进行进一步的编辑.

我已经设法将TextBoxes绑定到属性,将ComboBox ItemsSource绑定到Schools的ObservableCollection,SelectedItem绑定到SelectedSchool属性,并将Add按钮绑定到AddSchoolCommand,所以该集合确实被填充.

我无法完成的工作是找到一种绑定SelectedSchool属性的方法,这样我就可以通过ComboBox项列表修改现有的School对象,以及每次添加新学校时都从清除的TextBox开始.文本框既可以用作输入收集器,又可以显示现有的School数据.

所以这是我的方法:

// ViewModelBase

    internal void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
    }
    public event PropertyChangedEventHandler PropertyChanged; 

// ViewModel:

 public ObservableCollection<School> Schools { get; set; }
 public RelayCommand AddSchoolCommand { get; set; }

 string _SchoolName;
     public string SchoolName
            {
                get
                {
                    return _SchoolName;
                }
                set
                {
                    if (_SchoolName != value)
                    {
                        _SchoolName = value;
                        RaisePropertyChanged("SchoolName");
                    }
                }
            }

    string _SchoolAddress;
     public string SchoolAddress
            {
                get
                {
                    return _SchoolName;
                }
                set
                {
                    if (_SchoolAddress != value)
                    {
                        _SchoolAddress = value;
                        RaisePropertyChanged("SchoolAddress");
                    }
                }
            }

    string _SchooTelephone;
     public string SchooTelephone
            {
                get
                {
                    return _SchooTelephone;
                }
                set
                {
                    if (_SchooTelephone != value)
                    {
                        _SchoolAddress = value;
                        RaisePropertyChanged("SchooTelephone");
                    }
                }
            }

    object _SelectedSchool;
    public object SelectedSchool
    {
        get
        {
            return _SelectedSchool;
        }
        set
        {
            if (_SelectedSchool != value)
            {
                _SelectedSchool = value;
                RaisePropertyChanged("SelectedSchool");
            }
        }
    }

    public ViewModelResume()
    {
       Schools = new ObservableCollection<School>();
       AddSchoolCommand = new RelayCommand(AddSchool);
    }

    void AddSchool(Object obj)
    {
        Schools.Add(new School
        {
            Name = SchoolName,
            Address = SchoolAddress,
            Telephone = SchoolTelephone,
        });
    }

// XAML

<Grid Width="358" Height="135">
<TextBox x:Name="schoolName" Controls:TextBoxHelper.Watermark="School Name" Controls:TextBoxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="26" Margin="10,9,0,0" VerticalAlignment="Top" Width="166" Text="{Binding SchoolName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="schoolAddress" Controls:TextBoxHelper.Watermark="School Address" Controls:TextBoxHelper.ClearTextButton="True" Height="26" Margin="0,9,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="167" Text="{Binding School Address, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="schoolTelephone" Controls:TextBoxHelper.Watermark="School Telephone" Controls:TextBoxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="26" Margin="10,79,0,0" VerticalAlignment="Top" Width="133" Text="{Binding SchoolTelephone, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox x:Name="schoolsList" Text="Schools List" HorizontalAlignment="Left" Margin="10,155,0,0" VerticalAlignment="Top" Width="268" ToolTip="List of attended schools" IsEditable="True" IsReadOnly="True" ItemsSource="{Binding Schools}" SelectedItem="{Binding SelectedSchool}" DisplayMemberPath="SchoolName" />
<Button x:Name="saveSchoolButton" Content="Add" Margin="318,155,10,0" VerticalAlignment="Top" Height="26" Command="{Binding AddSchoolCommand}"/>
</Grid>

解决方法:

您的文本框正在侦听未意识到selectedSchool的视图模型中的属性.
您可以将文本框绑定到selectedSchool.Properties

首先创建您的SelectedSchool of Type School,并像这样发起它

School _SelectedSchool = new School();

然后更改所有文本框以绑定到选定的学校属性

<TextBox Text="{Binding SelectedSchool.SchoolName, UpdateSourceTrigger=PropertyChanged}"/>

然后当您添加

void AddSchool(Object obj)
{
    Schools.Add(SelectedSchool);
    SelectedSchool = New School();

}
上一篇:第一次单击组合框时,JavaFx组合框下拉列表的宽度较小


下一篇:c#-WPF-使用DataTemplate时通过键入选择ComboBox值