单击命令不适用于多个控件

时间:2015-01-15 12:02:28

标签: c# wpf mvvm

我的视图中有两个TextBox,一个DatePicker,一个ListBox和一个按钮。我想在按钮单击时将前三个控件的值绑定到ListBox。但是当我单击第三个文本框时,第二个文本框的值会自动添加到列表中(不单击按钮)。 DatePicker也存在同样的问题。

查看

<Grid>
    <DatePicker HorizontalAlignment="Left" Margin="111,49,0,0" VerticalAlignment="Top" Text="{Binding Customer1.Date, Mode=TwoWay}"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="111,78,0,0" TextWrapping="Wrap" Text="{Binding Customer1.Name, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding Customer1.Name1, Mode=TwoWay}" VerticalAlignment="Top" Width="120" Margin="111,106,0,0"/>
    <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.97,5.243" Margin="35,168,0,0" Command="{Binding ClickCommand}">
    </Button>
    <Label Content="{Binding Customer1.Name1}" HorizontalAlignment="Left" Margin="10,231,0,0" VerticalAlignment="Top" Width="107"/>
    <Label Content="{Binding Customer1.Name}" HorizontalAlignment="Left" Margin="10,269,0,0" VerticalAlignment="Top" Width="107"/>
    <Label Content="Scrum" HorizontalAlignment="Left" Margin="30,75,0,0" VerticalAlignment="Top"/>
    <Label Content="Standup" HorizontalAlignment="Left" Margin="30,103,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.105,0.591"/>
    <Label Content="Date" HorizontalAlignment="Left" Margin="30,49,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.87,0.395"/>
    <ListBox HorizontalAlignment="Left" Height="100" Margin="347,194,0,0" VerticalAlignment="Top" Width="100">
        <ListBoxItem Content="{Binding Customer1.Date}"/>
        <ListBoxItem Content="{Binding Customer1.Name1}"/>
        <ListBoxItem Content="{Binding Customer1.Name}"/>
    </ListBox>
</Grid>

视图模型

    public class CustomerViewModel:INotifyPropertyChanged
{
    public CustomerViewModel()
    {
        _Customer = new Customer();
    }
private Customer _Customer;
public Customer Customer1
{
get {return _Customer;}
}
private ICommand _clickCommand;
public ICommand ClickCommand
{
    get
    {
        return _clickCommand;
    }
    set
    {
        _clickCommand = value;
        OnPropertyChanged("ClickCommand");

    }
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{

    PropertyChangedEventHandler Handler = PropertyChanged;

    if (Handler != null)
    {

        Handler(this, new PropertyChangedEventArgs(propertyName));

    }

}

#endregion
}

模型

    public class Customer : INotifyPropertyChanged
{
private string _Name;
private string _Name1;
private string _Date;
public string Name
 {
 get{return _Name;}
 set
 {
 _Name=value;
 OnPropertyChanged("Name");
 }
 }
public string Name1
{
    get { return _Name1; }
    set
    {
        _Name1 = value;
        OnPropertyChanged("Name1");
    }
}
public string Date
{
    get { return _Date; }
    set
    {
        _Date = value;
        OnPropertyChanged("Date");
    }
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{

    PropertyChangedEventHandler Handler = PropertyChanged;

    if (Handler != null)
    {

        Handler(this, new PropertyChangedEventArgs(propertyName));

    }

}

#endregion


}

注意:我已将datacontext设置为我的viewmodel。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

TextBoxListBoxItem绑定到同一个对象。当其中一个更新对象时,有一个OnPropertyChanged,并通知UI进行更新。因此,如果文本TextBox更新Customer1.Name<ListBoxItem Content="{Binding Customer1.Name}"/>将会更新。

无论按钮动作是什么!

文本框在失去焦点时更新对象。您可以使用绑定属性ÙpdateSourceTrigger

更改它

如果您只想在单击按钮时更新ListBox,可能是您可以绑定另一个对象,并在命令操作上使用副本。

答案 1 :(得分:0)

您必须在项目中实施RelayCommand

public class RelayCommand : ICommand
{
#region Fields

readonly Action<object> _execute;
readonly Predicate<object> _canExecute;        

#endregion // Fields

#region Constructors

public RelayCommand(Action<object> execute)
: this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
    if (execute == null)
        throw new ArgumentNullException("execute");

    _execute = execute;
    _canExecute = canExecute;           
}
#endregion // Constructors

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
    return _canExecute == null ? true : _canExecute(parameter);
}

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameter)
{
    _execute(parameter);
}

 #endregion // ICommand Members
}

更改您的ICommand财产,如下所示&amp;实施CanCommandDo&amp; CommandDo次活动

    private ICommand _clickCommand;
    public ICommand ClickCommand
    {
        get
        {
            if (_clickCommand == null)
                _clickCommand = new RelayCommand(p => this.DoMyCommand(p),p => this.CanDoMyCommand(p));
            return _clickCommand;
        }
    }

    private bool CanDoMyCommand(object p)
    {
        ////enable or disable your command here
        return true;
    }

    private object DoMyCommand(object p)
    {
        ////operations of command execution goes here.
        return null;
    }