如果选择了ComboBox项目,C# WPF如何更改网格可见性?

3

我想在选中combobox的任何一项时,显示一个Grid及其内容。如果没有选择任何项,则该网格将保持隐藏。

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>

我尝试过以下代码:

XAML.CS

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

但这并没有改变任何事情。我已经尝试了多种方法,甚至使用了 string.IsNullOrEmpty

combobox 的源是一个 List<string>

编辑

该方法在这里被调用。

public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ChangeVisibility(cb, gr);
        }

ChangeVisibility 函数在哪里/如何被调用? - Peter M
看一下编辑,我已经添加了它。 - Koosshh56
1
你需要为ComboBox的SelectionChanged事件添加一个事件处理程序。这就是你的ChangeVisibility方法中的代码所属的位置。或者,你可以在XAML中将可见性属性绑定到SelectedItem属性,并使用IValueConverter来检查是否为空。 - Max Hampton
如果我使用SelectionChanged事件,它会在组合框中的每个项目上被调用吗?我的网格应该在没有选择时隐藏,在任何选择时可见。 - Koosshh56
1
每次 ComboBox 中的选择发生变化时,事件都会触发。因此,如果选择了任何项目,则网格将显示出来。如果所选项目变为 null(无选择),则网格将再次隐藏。 - Max Hampton
@MaxHampton,正是我所想的。谢谢。 - Koosshh56
2个回答

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

0

尝试使用

MainWindow.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:

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

或者更贴近你的问题:

MainWindow.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:

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

但我不确定你的方法是否正确。您无法轻松取消选择ComboBox中的项目,因此基本上如果选择任何内容,则网格将始终可见。我宁愿选择具有多个ComboBoxItem的解决方案,其中一个是“none”,“hide”或类似的东西。

您始终可以考虑MVVM模式,这在WPF框架中非常流行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接