Search code examples
wpfmvvmstackpanel

How to hide/show items in a stack panel?


I have a wpf-mvvm application.

In that I have follwoing...

<Combo box>
item 1
item 2
</Combo box>
<stack pnel>
 <user control 1 />
 <user control 1 />
</stack pnel>

If user select "item 1" from combo, i need to display "user control 1" If user select "item 2" from combo, i need to display "user control 2"

In the viewmodel...I have an IList of those two combobox items.

what is the best way to hid/show items here ?


Solution

  • You can actually remove the StackPanel completely, as you'll only be showing one UserControl at a time.

    Once you've done that, you can use the technique described here to bind the ComboBox's value to the visibility of the UserControl. Just set the Visibility to Collapsed for the UserControl that's not chosen.

    This allows you to handle this completely in XAML.