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

本文关键字:网格 可见性 C#WPF 选择 组合 项目 如果 | 更新日期: 2023-09-27 18:22:23

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.IsNullOrEmptycombobox的来源是List<string>

编辑

方法在这里被称为

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

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

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

尝试

主窗口.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;
    }
}

主窗口.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>

或者更类似于你的问题:

主窗口.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;
    }
}

主窗口.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>

但我不确定你的做法是否正确。您不能轻易地取消选择组合框中的项目,所以基本上,如果您选择任何内容,网格将始终可见。我宁愿选择具有多个ComboBoxItem的解决方案,其中一个将是"无"、"隐藏"或类似的内容。

您总是可以考虑MVVM模式,它在WPF框架中非常流行