简体   繁体   English

如果选择了组合框项,C# WPF 更改网格可见性

[英]C# WPF change grid visibility if combobox item is selected

I'm trying to display a Grid and its contents when an item (anyone) in combobox is selected. I'm trying to display a Grid and its contents when an item (anyone) in combobox is selected. If nothing is selected, grid will remain hidden.如果未选择任何内容,网格将保持隐藏状态。

XAML XAML

    <ComboBox x:Name="cb" HorizontalAlignment="Left" VerticalAlignment="Top" Width="140" Height="25"/>
    <Grid x:Name="gr" Visibility="Hidden">
            <Border BorderThickness="1" HorizontalAlignment="Left" Height="600"  VerticalAlignment="Top" Width="346">
                <Border  BorderThickness="1" RenderTransformOrigin="0.5,0.5">
            </Border>
    </Grid>

I've tried with this:我试过这个:

XAML.CS XAML文件

public void ChangeVisibility(ComboBox cb, Grid gr)
    {
        if (cb.SelectedItem != null)
        {
            gr.Visibility = Visibility.Visible;
        }
        else
        {
            gr.Visibility = Visibility.Hidden;
        }

But it doesn't change anything.但它不会改变任何东西。 I've tried in multiple ways, even with string.IsNullOrEmpty .我已经尝试了多种方式,即使使用string.IsNullOrEmpty Source of combobox is a List<string> . combobox来源是List<string>

EDIT编辑

Method is called here方法在这里被调用

public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ChangeVisibility(cb, gr);
        }
 <ComboBox x:Name="cb" HorizontalAlignment="Left" VerticalAlignment="Top" Width="140" Height="25"/>
    <Grid x:Name="gr">
        <Grid.Style>
            <Style TargetType="Grid">
                <Setter Property="Visibility" Value="Visible"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=cb, Path=SelectedItem}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
        <Border BorderThickness="1" HorizontalAlignment="Left" Height="600"  VerticalAlignment="Top" Width="346">
            <Border  BorderThickness="1" RenderTransformOrigin="0.5,0.5">
            </Border>
    </Grid>

Try with试试

MainWindow.xaml.cs主窗口.xaml.cs

private void ComboBox_Selected(object sender, RoutedEventArgs e)
{
    var item = Combo.SelectedItem as ComboBoxItem;
    if (item.Content.ToString() == "Visible")
    {
        RedGrid.Visibility = System.Windows.Visibility.Visible;
    }
    else

    {
        RedGrid.Visibility = System.Windows.Visibility.Hidden;
    }
}

MainWindow.xaml:主窗口.xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <ComboBox Name="Combo" SelectionChanged="ComboBox_Selected">
        <ComboBoxItem Content="Visible" />
        <ComboBoxItem Content="Hidden" />
    </ComboBox>
    <Grid Name="RedGrid" Grid.Row="1" Background="Red">

    </Grid>
</Grid>

Or to be more similar to your problem:或者更类似于您的问题:

MainWindow.xaml.cs:主窗口.xaml.cs:

private void ComboBox_Selected(object sender, RoutedEventArgs e)
{
    var item = Combo.SelectedItem as ComboBoxItem;
    if (item != null)
    {
        RedGrid.Visibility = System.Windows.Visibility.Visible;
    }
}

MainWindow.xaml:主窗口.xaml:

<ComboBox Name="Combo" SelectionChanged="ComboBox_Selected" >
    <ComboBoxItem Content="Element 1" />
    <ComboBoxItem Content="Element 2" />
</ComboBox>
<Grid Name="RedGrid" Grid.Row="1" Background="Red" Visibility="Hidden">

</Grid>

But I am not sure, that your approach is correct.但我不确定,你的方法是否正确。 You cannot easily deselect item in ComboBox so basically if you select anything Grid will be always visible.您无法轻松取消选择 ComboBox 中的项目,因此基本上,如果您选择任何内容,网格将始终可见。 I will rather chose solution with multiple ComboBoxItem where one of them will be "none", "hide" or sth like that.我宁愿选择具有多个 ComboBoxItem 的解决方案,其中一个将是“无”、“隐藏”或诸如此类。

You can always think about MVVM pattern which is very popular with WPF framework您总是可以考虑在 WPF 框架中非常流行的 MVVM 模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM