用命令绑定多个参数 [英] Multiple parameters with Command binding
问题描述
我有命令结合和利用Prism库中的文本块
I have a textblock with command binding and using Prism library.
这是XAML PARTH:
this is the XAML parth:
<TextBlock Margin="0,10,0,0">SocialSecurityNumer:</TextBlock>
<TextBox Name="SSNText" GotFocus="TextBox_GotFocus" Text="{Binding SSN, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0"/>
这是视图模型背后:
And this is the ViewModel behind:
public FindViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
}
public DelegateCommand FindCommand { get; set; }
private string ssn;
public string SSN
{
get { return ssn; }
set
{
if (ssn== value)
return;
ssn = value;
RaisePropertyChanged(() => SSN);
FindCommand.RaiseCanExecuteChanged();
}
}
这是侦听此事件触发的GridViewModel和火与SSN一个函数作为参数
And this is the GridViewModel that listen for this event trigger and fire up a function with SSN as a parameter
public class GridViewModel : NotificationObject
{
public GridViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
eventAggregator.GetEvent<SSNChangedEvent>().Subscribe(GetData);
}
public ObservableCollection<Investment> Investments { get; set; }
private void GetData(string ssn)
{
var list = GeniusConnection.GetDataFromWebService(ssn);
Investments = new ObservableCollection<Investment>(list);
RaisePropertyChanged(() => Investment);
}
}
我怎么可以添加其他参数,例如日期时间参数,这混淆了我的部分是:
How can i add another parameter, for example datetime parameter, the part that confuses me is:
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
本发布功能只需一个参数并为此我不知道怎样才能轻松地添加多个paramters ??
This Publish function takes just one parameter and therefor i don´t see how i can easily add multiple paramters.??
推荐答案
您应该创建包含要发布所有neccessary参数的一类。
you should create a class that holds all neccessary parameters that you want to publish.
public class SSNChangedEventParams
{
public string SSN{get;set;}
public DateTime Dt{get;set;}
...
}
和再发布的这个实例类:
and then Publish an instance of this class:
eventAggregator.GetEvent<SSNChangedEvent>().Publish(new SSNChangedEventParams(){SSN=SSN, Dt = DateTime.Now})
这篇关于用命令绑定多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!