为了简单起见,我认为有两个组合框。我想在遵循MVVM设计模式和PRISM 6框架的同时级联它们。继Brian Lagunas MVVM / Prism webinar之后,他提到了DelegateCommand
' s ObservesProperty
。我预感到我应该使用但不确定的东西。我提供了一个非常简单的View和ViewModel,我无法工作。
<ComboBox ItemsSource="{Binding PackageNames}" SelectedItem="{Binding PackageNameSelected}" />
<ComboBox ItemsSource="{Binding PackageOptions}"/>
public MainWindowViewModel() // Constructor
{
PackageNamesCommand = new DelegateCommand(PackageNamesCommandExecute);
PackageOptionsCommand = new DelegateCommand(PackageOptionsCommandExecute).ObservesProperty(() => PackageNameSelected);
}
private string _packageNameSelected;
public string PackageNameSelected
{
get { return _packageNameSelected; }
set { SetProperty(ref _packageNameSelected, value); }
}
private ObservableCollection<string> _packeNames;
public ObservableCollection<string> PackageNames
{
get { return _packeNames; }
set { SetProperty(ref _packeNames, value); }
}
private ObservableCollection<string> _packageOptions;
public ObservableCollection<string> PackageOptions
{
get { return _packageOptions; }
set { SetProperty(ref _packageOptions, value); }
}
public DelegateCommand PackageNamesCommand { get; set; }
private async void PackageNamesCommandExecute()
{
await Task.Factory.StartNew(() => GetPackageNames()).ContinueWith(t => PackageNames = ToObservableCollection(t.Result));
}
private IQueryable<string> GetPackageNames()
{
return //My LINQ Query Here
}
public DelegateCommand PackageOptionsCommand { get; set; }
private void PackageOptionsCommandExecute()
{
Task.Factory.StartNew(() => GetPackageOptions()).ContinueWith(t => PackageOptions = ToObservableCollection(t.Result));
}
private IQueryable<string> GetPackageOptions()
{
return // My LINQ query here
}
答案 0 :(得分:3)
这不是一个真正的棱镜问题。要完成此任务,您只需将代码放在PackageNameSelected属性的setter中,然后调用您的方法以生成相关的选项集合。还有其他方法可以做到,但这是最直接的。