HiTech
HiTech

Reputation: 1003

WPF Cascading Combobox using PRISM 6

To keep things simple, I have two comboboxes on my view. I would like to cascade them while adhering to the MVVM design pattern and the PRISM 6 framework. Following Brian Lagunas MVVM/Prism webinar he mentions the DelegateCommand's ObservesProperty. I have a hunch that's something I should be using but not sure. I provided a very simple View and ViewModel that I can't get working.

XAML (View):

<ComboBox ItemsSource="{Binding PackageNames}" SelectedItem="{Binding PackageNameSelected}" />
<ComboBox ItemsSource="{Binding PackageOptions}"/>

ViewModel:

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
}

Upvotes: 1

Views: 662

Answers (1)

user5420778
user5420778

Reputation:

This isn't really a Prism issue. To accomplish this task you simply need to place code in the setter of the PackageNameSelected property and then call your method to generate the dependent collection of options. There are other ways to do it, but this is the most straight forward.

Upvotes: 3

Related Questions