Reputation: 60
I'm trying to implement a functionality where user can select type of view on Combobox. Based on selection either DataGrid or ListView will display given data.
My question here is: What is the correct way to implement this?
Do I set Visibility property on DataGrid and ListView based on selection of the Combobox or is there another "cleaner" way to do this (referring to clean-code principles)?
Upvotes: 0
Views: 95
Reputation: 169228
You could use a ContentControl
with a Style
that triggers on the selected value in the ComboBox
, e.g.:
<ComboBox x:Name="cmb" xmlns:s="clr-namespace:System;assembly=System.Runtime"
SelectedIndex="0">
<s:String>DataGrid</s:String>
<s:String>ListBox</s:String>
</ComboBox>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Content">
<Setter.Value>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="..." />
</DataGrid.Columns>
</DataGrid>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, ElementName=cmb}" Value="ListBox">
<Setter Property="Content">
<Setter.Value>
<ListBox>
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
</ListBox>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
Upvotes: 1