我在我的視圖中有兩個文本框,一個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的我視圖模型。任何幫助讚賞。
由於我是新來的MVVM,你能解釋一下解決? – user3065219