I would like to display a different stackpanel based on the selection made in a combobox. Idea is to collapse any stackpanels that aren't needed.
The combobox looks something like this:
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock Text="New Question" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center" Margin="10"/>
<ComboBox x:Name="ComboBox" MaxWidth="200" IsTextSearchEnabled="True" SelectedValuePath="Choice">
<ComboBox.Items>
<ComboBoxItem>Test</ComboBoxItem>
<ComboBoxItem>SliderQuestion</ComboBoxItem>
<ComboBoxItem>OpenQuestion</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</StackPanel>
So if Sliderquestion is selected, I want to display the stackpanel containing the sliderquestion setup elements.
I got a basic stackpanel setup right now which looks like this.
<StackPanel Grid.Column="1" Grid.Row="1">
<StackPanel.Resources>
<Style x:Key="ForSliderQuestion" TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ComboBox, Path=SelectedValue}" Value="SliderQuestion">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ComboBox, Path=SelectedValue}" Value="OpenQuestion">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<StackPanel Style="{DynamicResource ForSliderQuestion}">
<TextBlock Text="Test" FontSize="30"></TextBlock>
</StackPanel>
</StackPanel>
I honestly think this shouldn't be so hard but I'm still a newb so what do I know. Anyone willing to help? Thanks!
EDIT, SOLUTION:
Alright, with the help of DrkDeveloper I figured it out.
I used his converter. Just copy pasted it. Thing was, his binding kept throwing an "Object reference not set on an instance of an object" error. I had this before. After scrolling through the recommended options given by visual studio, I changed it to this and it worked!
<StackPanel>
<TextBlock Text="New Question" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center" Margin="10"></TextBlock>
<ComboBox x:Name="_combo" MaxWidth="200" IsTextSearchEnabled="True">
<ComboBoxItem Content="SliderQuestion"></ComboBoxItem>
<ComboBoxItem Content="OpenQuestion"></ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Visibility="{Binding SelectionBoxItem, ElementName=_combo,
Converter={StaticResource ItemToVisibilityConverter}, ConverterParameter=SliderQuestion}">
<TextBlock Text="TestTest"></TextBlock>
</StackPanel>
Apparantly it needs to specifically know what sort of item it is. A SelectionBoxItem.
Something like this?
<Window.Resources>
<loc:ItemToVisibilityConverter x:Key="itemToVisibilityConverter"></loc:ItemToVisibilityConverter>
</Window.Resources>
<Grid x:Name="_masterContainer">
<ComboBox x:Name="_combo">
Test
Slider
QuestionName
</ComboBox>
<StackPanel Visibility="{Binding SelectedItem, Converter={StaticResource itemToVisibilityConverter}, ConverterParameter=QuestionName, ElementName=_combo}">
</StackPanel>
<StackPanel Visibility="{Binding SelectedItem, Converter={StaticResource itemToVisibilityConverter}, ConverterParameter=Slider, ElementName=_combo}">
</StackPanel>
<StackPanel Visibility="{Binding SelectedItem, Converter={StaticResource itemToVisibilityConverter}, ConverterParameter=Test, ElementName=_combo}">
</StackPanel>
</Grid>
Choose SelectedItem or SelectedValue for your purposes...
With this:
public class ItemToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ //Make the required checks here. if you content is comboboxitem or something you have to make the conversion here.
if (value.ToString().Equals(parameter.ToString()))
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Edit: Added style way:
<Window.Resources>
<loc:ItemToVisibilityConverter x:Key="itemToVisibilityConverter"></loc:ItemToVisibilityConverter>
<Style x:Key="sliderStyle" TargetType="StackPanel">
<Setter Property="Visibility" Value="Collapsed"/> <!-- this is important-->
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedValue, ElementName=_combo, Converter={StaticResource itemToVisibilityConverter}, ConverterParameter=Slider}">
<DataTrigger.Setters>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="_masterContainer">
<ComboBox x:Name="_combo">
Test
Slider
QuestionName
</ComboBox>
<StackPanel Visibility="{Binding SelectedItem, Converter={StaticResource itemToVisibilityConverter}, ConverterParameter=QuestionName, ElementName=_combo}">
</StackPanel>
<StackPanel Style="{StaticResource sliderStyle}">
</StackPanel>
</Grid>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.