WPF:尝试根据组合框中的选择更改可见性

WPF: Trying to change visibility based on selection in combobox

我想根据在组合框中所做的选择显示不同的堆栈面板。 想法是折叠任何不需要的堆栈面板。

组合框看起来像这样:

<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>

因此,如果选择了 Sliderquestion,我想显示包含 sliderquestion 设置元素的堆栈面板。

我现在有一个基本的堆栈面板设置,看起来像这样。

<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>

老实说,我认为这不应该那么难,但我还是个新手,所以我知道什么。有人愿意帮忙吗? 谢谢!

编辑,解决方案:

好的,在 DrkDeveloper 的帮助下我弄明白了。

我用了他的转换器。复制粘贴就行了。 问题是,他的绑定不​​断抛出 "Object reference not set on an instance of an object" 错误。我以前有过这个。 在滚动浏览 visual studio 给出的推荐选项后,我将其更改为这个并且有效!

<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>

显然它需要明确知道它是什么类型的物品。 一个 SelectionBoxItem。

是这样的吗?

<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>

根据您的目的选择 SelectedItem 或 SelectedValue...

有了这个:

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();
        }
    }

编辑:添加样式方式:

<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>