What is the most minimal way to dynamically interchange UIElements at a certain ‘slot’ on the UI? I have a number of UIElements and based on some external databinding (e.g. a combobox selection) I want one of them to be shown and the one that was currently visible to be hidden. So the behavior is sort of like the TabControl but without the tabstrip, the chrome and the tabitem instances. So I could actually use the TabControl and override the control template. But is this really the most minimal approach?
What is the most minimal way to dynamically interchange UIElement s at a certain
Share
You have several options. As Bryan mentioned, Data Template Selectors can definitely work, although I’ve decided that they are frequently overkill. Take your example for instance – if you want to bind the visibility of an element to the selection in a
ComboBox, I would prefer aValueConverterinstead. Pass theComboBox.SelectedItemto theConverterand have it return a Visibility value:Now in your Window.Resources, create an instance of the
ValueConverterand Bind the Element to theComboBox.SelectedItemusing theValueConverterinstance:And of course you can reuse the
ValueConverterfor all the elements in yourDataTemplate(and if there are a lot of them, then the Data Template Selector approach becomes more desirable).[Disclaimer: the code above was hashed out from memory and not tested – it may need a little tweaking]