2015-01-15 26 views
0

我在我的視圖中有兩個文本框,一個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的我視圖模型。任何幫助讚賞。

回答

0

TextBoxListBoxItem綁定到同一個對象。當他們中的一個更新對象時,有一個OnPropertyChanged,並通知用戶界面進行更新。所以如果文字TextBox更新Customer1.Name,<ListBoxItem Content="{Binding Customer1.Name}"/>將被更新。

無論按鈕操作如何!

文本框丟失焦點時更新對象。您可以使用綁定屬性更改它ÙpdateSourceTrigger

如果要僅在您單擊按鈕時更新ListBox,可能是您可以綁定另一個對象,並在命令操作中使用副本。

+0

由於我是新來的MVVM,你能解釋一下解決? – user3065219

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屬性像如下&實施CanCommandDo & 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; 
    } 
+0

感謝您的建議。但它無法解決我的問題? – user3065219

相關問題