none
CommandParamter的使用问题 RRS feed

  • 问题

  • 我使用mvvm模式,想实现的功能是根据用户在textbox上输入的age和name信息,点击button之后就add到list中。界面代码如下:

    <TextBox x:Name="tAge" Width="40" ></TextBox>
    <TextBox x:Name="tName" Width="40" ></TextBox>

    <Button x:Name="button1" Content="AddPerson" Command="{Binding AddCommand}" CommandParameter="{???} "></Button>

     

    Addcommand的定义:  ICommand AddCommand = new DelegateCommand<Person>(p => Customers.Add(p));

     

    person类的定义:

     public class Person
        {
            public int Age
            { get; set; }

            public string Name
            { get; set; }
        }

    现在的问题就是我要将用户输入的age和name拼装成一个person对象再传给AddCommand,不知道这个拼装的语法应该怎么写?msdn上也没有查到相关的例子。

     

     


    they say nothing last forever
    2011年7月20日 18:33

答案

  • 你好,

    要解决这个问题,可以使用MultiBinding来绑定2个对象(Age和Name)并转换成一个Person对象。

    这里有简单的MultiBinding的例子。你可以参照一下。

    如果有任何不清楚的地方请尽管跟帖补充。

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年7月22日 9:20
    版主
  • 应该将当前的编辑对象Person作为ViewModel的成员,提交时判断CurrentPerson是否正确有值,然后加入集合。

    window:

    <Window x:Class="PersonManger.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:vm="clr-namespace:PersonManger"
        Title="MainWindow" Height="190" Width="347">
    	<Window.DataContext>
    		<vm:MainWindowViewModel />
    	</Window.DataContext>
    	<Window.Resources>
    		<DataTemplate DataType="{x:Type vm:Person}">
    			<TextBlock><TextBlock Text="{Binding Name}" />(<TextBlock Text="{Binding Age}" />)</TextBlock>
    		</DataTemplate>
    	</Window.Resources>
      <Grid>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,44,0,0" Name="textBlock1" Text="Name:" VerticalAlignment="Top" />
    		<TextBox Height="23" HorizontalAlignment="Left" Margin="103,41,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"
    				 Text="{Binding CurrentPerson.Name,Mode=TwoWay}"/>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,73,0,0" Name="textBlock2" Text="Age:" VerticalAlignment="Top" />
    		<TextBox Height="23" HorizontalAlignment="Left" Margin="103,70,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" 
    				 Text="{Binding CurrentPerson.Age,Mode=TwoWay}"/>
    		<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="104,104,0,0" Name="button1" VerticalAlignment="Top" Width="75" 
    				Command="{Binding AddPersonCommand}"/>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,12,0,0" Name="textBlock3" Text="Person" VerticalAlignment="Top" />
    		<ComboBox Height="23" HorizontalAlignment="Left" Margin="103,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" 
    				 ItemsSource="{Binding Persons}"/>
    	</Grid>
    </Window>
    
    


    ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using System.Windows;
    
    namespace PersonManger {
    	public class MainWindowViewModel : INotifyPropertyChanged {
    
    		public ObservableCollection<Person> Persons { get; private set; }
    		public event PropertyChangedEventHandler PropertyChanged;
    		private Person _currentPerson;
    		///<summary>
    		///
    		///</summary>
    		public Person CurrentPerson {
    			get { return _currentPerson; }
    			set {
    				if (_currentPerson != value) {
    					_currentPerson = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentPerson"));
    				}
    			}
    		}
    
    		public MainWindowViewModel() {
    			Persons = new ObservableCollection<Person>();
    			Persons.Add(new Person { Name = "Kevin", Age = 28 });
    			Persons.Add(new Person { Name = "Jerry", Age = 27 });
    
    			CurrentPerson = new Person();
    		}
    
    		public ICommand AddPersonCommand {
    			get {
    				return new DelegateCommand(() => {
    					if (CurrentPerson!=null && !string.IsNullOrWhiteSpace(CurrentPerson.Name)) {
    						Persons.Add(CurrentPerson);
    						CurrentPerson = new Person();
    						MessageBox.Show("Add Success!");
    					}
    				});
    			}
    		}
    
    		
    	}
    }
    
    


    Person类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace PersonManger {
    	public class Person : INotifyPropertyChanged {
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		private string _name;
    		///<summary>
    		///
    		///</summary>
    		public string Name {
    			get { return _name; }
    			set {
    				if (_name != value) {
    					_name = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name"));
    				}
    			}
    		}
    
    		private int _age;
    		///<summary>
    		///
    		///</summary>
    		public int Age {
    			get { return _age; }
    			set {
    				if (_age != value) {
    					_age = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Age"));
    				}
    			}
    		}
    		
    	}
    }
    
    

    效果图:
    http://hi.csdn.net/attachment/201107/23/63407_131141980011pO.png
    http://hi.csdn.net/attachment/201107/23/63407_1311419815Bp0l.png
    http://hi.csdn.net/attachment/201107/23/63407_1311419827ZVcq.png


    Sonny.Lin
    2011年7月23日 11:17

全部回复

  • 你好,

    要解决这个问题,可以使用MultiBinding来绑定2个对象(Age和Name)并转换成一个Person对象。

    这里有简单的MultiBinding的例子。你可以参照一下。

    如果有任何不清楚的地方请尽管跟帖补充。

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年7月22日 9:20
    版主
  • 应该将当前的编辑对象Person作为ViewModel的成员,提交时判断CurrentPerson是否正确有值,然后加入集合。

    window:

    <Window x:Class="PersonManger.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:vm="clr-namespace:PersonManger"
        Title="MainWindow" Height="190" Width="347">
    	<Window.DataContext>
    		<vm:MainWindowViewModel />
    	</Window.DataContext>
    	<Window.Resources>
    		<DataTemplate DataType="{x:Type vm:Person}">
    			<TextBlock><TextBlock Text="{Binding Name}" />(<TextBlock Text="{Binding Age}" />)</TextBlock>
    		</DataTemplate>
    	</Window.Resources>
      <Grid>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,44,0,0" Name="textBlock1" Text="Name:" VerticalAlignment="Top" />
    		<TextBox Height="23" HorizontalAlignment="Left" Margin="103,41,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"
    				 Text="{Binding CurrentPerson.Name,Mode=TwoWay}"/>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,73,0,0" Name="textBlock2" Text="Age:" VerticalAlignment="Top" />
    		<TextBox Height="23" HorizontalAlignment="Left" Margin="103,70,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" 
    				 Text="{Binding CurrentPerson.Age,Mode=TwoWay}"/>
    		<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="104,104,0,0" Name="button1" VerticalAlignment="Top" Width="75" 
    				Command="{Binding AddPersonCommand}"/>
    		<TextBlock Height="23" HorizontalAlignment="Left" Margin="47,12,0,0" Name="textBlock3" Text="Person" VerticalAlignment="Top" />
    		<ComboBox Height="23" HorizontalAlignment="Left" Margin="103,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" 
    				 ItemsSource="{Binding Persons}"/>
    	</Grid>
    </Window>
    
    


    ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using System.Windows;
    
    namespace PersonManger {
    	public class MainWindowViewModel : INotifyPropertyChanged {
    
    		public ObservableCollection<Person> Persons { get; private set; }
    		public event PropertyChangedEventHandler PropertyChanged;
    		private Person _currentPerson;
    		///<summary>
    		///
    		///</summary>
    		public Person CurrentPerson {
    			get { return _currentPerson; }
    			set {
    				if (_currentPerson != value) {
    					_currentPerson = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentPerson"));
    				}
    			}
    		}
    
    		public MainWindowViewModel() {
    			Persons = new ObservableCollection<Person>();
    			Persons.Add(new Person { Name = "Kevin", Age = 28 });
    			Persons.Add(new Person { Name = "Jerry", Age = 27 });
    
    			CurrentPerson = new Person();
    		}
    
    		public ICommand AddPersonCommand {
    			get {
    				return new DelegateCommand(() => {
    					if (CurrentPerson!=null && !string.IsNullOrWhiteSpace(CurrentPerson.Name)) {
    						Persons.Add(CurrentPerson);
    						CurrentPerson = new Person();
    						MessageBox.Show("Add Success!");
    					}
    				});
    			}
    		}
    
    		
    	}
    }
    
    


    Person类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace PersonManger {
    	public class Person : INotifyPropertyChanged {
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		private string _name;
    		///<summary>
    		///
    		///</summary>
    		public string Name {
    			get { return _name; }
    			set {
    				if (_name != value) {
    					_name = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name"));
    				}
    			}
    		}
    
    		private int _age;
    		///<summary>
    		///
    		///</summary>
    		public int Age {
    			get { return _age; }
    			set {
    				if (_age != value) {
    					_age = value;
    					if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Age"));
    				}
    			}
    		}
    		
    	}
    }
    
    

    效果图:
    http://hi.csdn.net/attachment/201107/23/63407_131141980011pO.png
    http://hi.csdn.net/attachment/201107/23/63407_1311419815Bp0l.png
    http://hi.csdn.net/attachment/201107/23/63407_1311419827ZVcq.png


    Sonny.Lin
    2011年7月23日 11:17