由于另一个组合框字符串值,如何更改组合框上的项目?

Posted

技术标签:

【中文标题】由于另一个组合框字符串值,如何更改组合框上的项目?【英文标题】:How can I change items on a combobox due to another combobox string value? 【发布时间】:2021-04-12 10:50:27 【问题描述】:

On this image

我必须从顶部组合框中选择一个类别(例如冒险、经典、科幻),然后书籍将根据该类别在第二个组合框中。我对此了解不多,所以我需要帮助来理解。

C#代码

public SelectCategories()
    
        InitializeComponent();
        
        Cat.Items.Add("Adventure");
        Cat.Items.Add("Classic");
        Cat.Items.Add("Science Fiction");
        
        if (Cat.Text == "Adventure")
        
            Book.Items.Add("a");
            Book.Items.Add("d");
            Book.Items.Add("v");
        
        else if(Cat.Text == "Classic")
        
            Book.Items.Add("c");
            Book.Items.Add("l");
            Book.Items.Add("s");
        
    

Xaml 代码

<ComboBox x:Name="Cat" HorizontalAlignment="Left" Margin="228,66,0,0" VerticalAlignment="Top" Width="186" Height="32" SelectionChanged="ComboBox_SelectionChanged"/>
    <ComboBox x:Name="Book" HorizontalAlignment="Left" Margin="228,142,0,0" VerticalAlignment="Top" Width="186" Height="32" SelectionChanged="ComboBox_SelectionChanged_1"/>
    <Label Content="Select a Category:" FontSize="16" HorizontalAlignment="Left" Margin="34,66,0,0" VerticalAlignment="Top" Height="32" Width="162"/>
    <Label Content="Select a Book:" FontSize="16" HorizontalAlignment="Left" Margin="34,142,0,0" VerticalAlignment="Top" Height="32" Width="162"/>
    <Button Content="Add Book to List.." HorizontalAlignment="Left" Margin="34,236,0,0" VerticalAlignment="Top" Width="177" Height="32" Click="Button_Click"/>
    <CheckBox Content="Confirm" HorizontalAlignment="Left" Margin="54,316,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
    <Button Content="Next" HorizontalAlignment="Left" Margin="306,356,0,0" VerticalAlignment="Top" Width="113" Height="32" Click="Button_Click_1"/>

【问题讨论】:

在此处查看示例***.com/questions/3063320/… 非常感谢! 【参考方案1】:

使用 ViewModel 和绑定更容易,直接使用来自代码后面的控件不是好方法

查看模型

public class CategoryModel

    public string Name  get; set; 
    public IList<string> Books  get; set; 


public class YourViewModel

    public ObservableCollection<CategoryModel> Categories  get; set;  = new ObservableCollection<CategoryModel>
    
        new CategoryModel  Name = "Adventure", Books = new List<string>"a", "d", "v",
        new CategoryModel  Name = "Classic", Books = new List<string>"c", "l", "s" ,
        new CategoryModel  Name = "Science Fiction", Books = new List<string>"s", "c", "f" 
    ;

    public CategoryModel SelectedCategory  get; set; 

xaml

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTestApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    d:DataContext="d:DesignInstance Type=local:YourViewModel, IsDesignTimeCreatable=True">
<StackPanel>
    <ComboBox ItemsSource="Binding Categories"
              DisplayMemberPath="Name"
              SelectedItem="Binding SelectedCategory"/>
    <ComboBox ItemsSource="Binding SelectedCategory.Books"/>
</StackPanel>

【讨论】:

非常感谢!我会试试这个。 @m8beacoder *** 文化意味着您的投票或标记为可接受的答案,而不是说谢谢。【参考方案2】:

我在不使用绑定的情况下解决了这个问题,这里是 c# 代码:

public SelectCategories()
    
        InitializeComponent();
        Cat.Items.Add("Adventure");
        Cat.Items.Add("Classic");
        Cat.Items.Add("Science Fiction");
        next.IsEnabled = false;

    

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    

        

        switch (e.AddedItems[0].ToString())
        
            case "Adventure":
                Books.Items.Clear();
                Books.Items.Add("Journey to the Center of the Earth");
                Books.Items.Add("The Count of Monte Cristo");
                Books.Items.Add("Don Quixote");
                break;
            case "Classic":
                Books.Items.Clear();
                Books.Items.Add("The Great Gatsby");
                Books.Items.Add("Fahrenheit 451");
                Books.Items.Add("Alice's Adventures in Wonderland");
                break;
            case "Science Fiction":
                Books.Items.Clear();
                Books.Items.Add("Altered Carbon");
                Books.Items.Add("Brave New World");
                Books.Items.Add("Frankenstein");
                break;
            default:
                break;
        
    

xaml 相同。

【讨论】:

以上是关于由于另一个组合框字符串值,如何更改组合框上的项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 上使用数据库数据获取组合框上的范围值

使用带有排序组合框和页面组合框上的许多项目的列表视图

访问 2007 组合框上的选定值未显示在多个记录上

如何在代码隐藏中更改组合框上的选定项

从组合框上的多个选择中获取值

组合框上的参数值