0

I have a combobox displaying the lists within it perfectly in the ViewModel but I'm looking to have it so that when a selected item from the list is chosen it fires the ViewModel screen and I only want one from the list to do this? So here is what I have in the ChooseView:

<ComboBox x:Name="CatalogName1" SelectedItem="{Binding SelectedCatalog1}" Style="{DynamicResource appComboBox}" Grid.Row="1" Grid.Column="1"  >
    </ComboBox>

and in the ChooseViewModel:

public List<string> CatalogName1
    {
        get
        {
            return new List<string> { "New", "Replace", "Extended", "Nothing", "ShowScreen" };
        }


    }

     private string selectedCatalog1;
    public string SelectedCatalog1
    {
        get
        {
            return this.selectedCatalog1;
        }

        set
        {
            this.selectedCatalog1 = value;
            this.NotifyOfPropertyChange(() => this.SelectedCatalog1);
        }
    }

the "ShowScreen" in the combo list should display the ShowScreenViewModel but I have tried with the getter setter and it's not making sense to me

  • You should make a method who checks if your `value` in `set` is equal to `ShowScreen`. If so, then activate your `ViewModel`, else just set the text, and call notify. – sharp Oct 04 '17 at 13:34
  • How would I go about that method, sorry I should have said I'm new to all this binding and with caliburn? –  Oct 04 '17 at 14:18
  • You mean how to activate your ViewModel or how to check if you clicked ShowScreen? – sharp Oct 04 '17 at 14:21
  • A cavalcade of nope. Convert your collection property into a { get; private set; }. Returning a new collection each time is terrible. You're also not binding it to ItemsSource, so how do you expect anything to show in the combo box? –  Oct 04 '17 at 17:06
  • @arcticwhite how to check if its been clicked, I'm guessing itemsource as I already have selecteditem, none of the example show how to display a new screen. If I had click button it would be a and public void ShowScreen() { ActivateItem(ShowScreenViewModel.getinstance()); } –  Oct 04 '17 at 22:19

1 Answers1

1

Okay, this is the way I would fix the problem...

private string selectedCatalog1;
    public string SelectedCatalog1
    {
        get
        {
            return selectedCatalog1;
        }
        set
        {
            selectedCatalog1 = value;
            ValidateValue(value);
            NotifyOfPropertyChange(() => SelectedCatalog1);
        }
    }
    private void ValidateValue(string s)
    {
        if (s == "ShowScreen")
        {
            ActivateItem(new ShowScreenViewModel());
        }
    }
sharp
  • 1,191
  • 14
  • 39