如何在mvvm命令

时间:2015-05-19 09:27:54

标签: c# wpf xaml mvvm icommand

我还在搞清楚mvvm,我想通过拆分模型视图模型和视图来使我的应用程序正确。在过去,我总是使用mytextbox.Text直接获取视图中文本框的值。

我在viewmodel中创建了一个命令,用于向网络添加新人。但是我无法将文本框的值放入viewmodel中的命令中。

这是我到目前为止的代码 在模型中

public class Person : INotifyPropertyChanged
{
    public Person()
    { }

    public Person(String FirstName)
    {
        this._firstName = FirstName;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private string _firstName;

    public string FirstName    // the Name property
    {
        get { return this._firstName; }
        set { this._firstName = value; NotifyPropertyChanged("FirstName"); }
    }
}

在我有的视图模型中

public class NetworkViewModel: INotifyPropertyChanged
{

    private ObservableCollection<Person> _networkList1 = new ObservableCollection<Person>();
    public ObservableCollection<Person> NetworkList1 //Binds with the listbox
    {
        get { return _networkList1; }
        set { _networkList1 = value; RaisePropertyChanged("NetworkList1"); }
    }

     public NetworkViewModel() 
    {
        AddPersonCommand = new RelayCommand(AddPerson);
    }

    private ICommand _addPersonCommand;
    public ICommand AddPersonCommand // Binding with view
    {
        get { return _addPersonCommand; }
        set { _addPersonCommand = value; }
    }

   public void AddPerson(object obj)
    {

    if(cb_group.Text.ToUpper() == "PRIMARY")
    {
        _networkList1.Add(new Person(){ FirstName = tb_firstName.Text,});
    }
}

在XAML中

 <TextBox x:Name="tb_firstName"  Text="{Binding Path=FirstName}"/>

<Button x:Name="btn_add" Command="{Binding AddPersonCommand }"/>

我想要的是使tb_firstname和cb_group的值在viewmodel中使用,这样我就可以使命令工作。 感谢你的帮助。我就是在学习的时候。

3 个答案:

答案 0 :(得分:1)

好的,所以你现在的文本框中有:

<TextBox x:Name="tb_firstName"  Text="{Binding Path=FirstName}"/>

这意味着它正在查看名为FirstName的viewmodel上的属性,因此您需要为viewmodel添加一个字符串属性。然后,当您在新Person上设置FirstName时,只需将其设置为属性。

new Person() { FirstName = this.FirstName };

对于绑定组合框,它需要一个itemsSource - 这是项目的集合,在你的情况下是要显示的NetworkList1。由于它绑定到对象列表,您还需要设置DisplayMemberPath - 这是您要在组合中显示的属性的名称。 您还需要绑定SelectedItem属性并在viewmodel中创建一个属性来存储它。这就是您知道当前选择列表中哪个项目的方式。

<Combobox ItemsSource={Binding Path=NetworkList1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
SelectedItem = {Binding Path= SelectedPerson, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}
DisplayMemberPath=FirstName/>

视图模型:

public Person SelectedPerson
{
  //put in get/set/propertychanged 
}

public string FirstName
{
   //put in get/set/propertychanged
}

答案 1 :(得分:0)

您需要添加一个CommandParameter并将其绑定到particualr Element。(在您的情况下为tb_firstname。

TextBox的版本:

<Button Command="{Binding AddPersonCommand } CommandParameter="{Binding ElementName=tb_firstname, Path=Text"/>

EDIT。 ComboBox的版本:

<Button Command="{Binding AddPersonCommand } CommandParameter="{Binding ElementName=yourComboBox, Path=SelectedItem.Value"/> // or just SelectedItem

EDIT2 来自(Multiple command parameters wpf button object

<Button Content="MultiBindingExample" Command="{Binding MyCommand}">
 <Button.CommandParameter>
     <MultiBinding Converter="{StaticResource MyMultiConverter}">
         <Binding Path="..." ElementName="MyTextBox"/>
         <Binding Path="..." ElementName="MySomethingElse"/>
     </MultiBinding>
 </Button.CommandParameter>

答案 2 :(得分:0)

你应该做的是在AddPerson方法中,而不是在控件中查找值(cb_group.Text)使用这些控件绑定的值。默认情况下,绑定应该是2路。因此,当用户更改值时,它将在模型/视图模型中自动更新。如果没有发生这种情况,则问题在于您的数据绑定,您应该查看控制台输出中是否有任何数据绑定错误。