【问题标题】:use of combobox to appear another combobox C#使用组合框出现另一个组合框 C#
【发布时间】:2014-10-07 04:54:24
【问题描述】:

我想这样做,一旦我选择了first_ComboBox 的第一项(Student Information),就会出现second_ComboBox

我怎样才能做到这一点

在cs代码中

private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void second_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

在 XAML 中

<StackPanel Margin="97,47,171,499" Orientation="Horizontal" Grid.Row="1">
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Where You want to Control" VerticalAlignment="Top" Height="82" Width="463" FontSize="36"/>
        <ComboBox x:Name="first_ComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="560" Height="42" SelectionChanged="first_ComboBox_SelectionChanged">
            <x:String>Student Information</x:String>
            <x:String>Staff Information</x:String>
            <x:String>Academic Information</x:String>
        </ComboBox>
    </StackPanel>
    <StackPanel Margin="97,172,171,374" Orientation="Horizontal" Grid.Row="1">
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Select the Field" VerticalAlignment="Top" Height="82" Width="463" FontSize="36"/>
        <ComboBox x:Name="second_ComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="560" Height="42" SelectionChanged="second_ComboBox_SelectionChanged">
            <x:String>Student Name</x:String>
            <x:String>Student Address</x:String>                      
        </ComboBox>
    </StackPanel>

【问题讨论】:

  • 你想在两个组合框上显示相同的信息吗?
  • 不,我希望一旦我选择了第一个组合框的第一项,就想显示第二个组合框
  • 在两个不同的列表框中嵌入堆栈面板,然后在您喜欢的任何事件上使用 c# 代码使其可见或隐藏它 lstEvents.Visibility = System.Windows.Visibility.Collapsed; prLoading.Visibility = System.Windows.Visibility.collapsed;
  • 聪明的开发者我该怎么做?

标签: c# xaml combobox windows-store-apps windows-8.1


【解决方案1】:

在您的 Form main 上,您可以将 Second Combobox 的可见性设置为 false,然后在第一个组合框选择更改时将其设置为 true,就像这样

  public MainWindow()
    {
        InitializeComponent();
        second_ComboBox.Visibility = Visibility.Collapsed;
    }

    private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selecteditem = first_ComboBox.SelectedItem as string;
        if (selecteditem != null)
        {
            second_ComboBox.Visibility = Visibility.Visible;
        }
    }

【讨论】:

  • 是的,它的工作,但我也想知道它在学生信息选择 span>时如何工作
  • @sahanYugantha 因为它触发了第一个组合框的选择更改事件
  • 我们不能进行自定义选择吗?打开不同的组合框
  • 如果你能给出一些提示或示例如何做到这一点
【解决方案2】:

在初始化时隐藏第二个组合框可见性

    public MainWindow()
{
    InitializeComponent();
    second_ComboBox.Visibility = Visibility.Collapsed;
}

private void first_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
       first_ComboBox.Visibility = System.Windows.Visibility.Visible;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2013-04-25
    相关资源
    最近更新 更多