0

On this image

I have to choose a category from top combobox (for ex. Adventure, Classic, Science Fiction) then books will be on second combobox based on the category. I don't have much knowledge about this so I need help to understand.

C# code

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 code

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

2 Answers2

0

It's easier with ViewModel and binding, direct working with controls from code behind isn't good approach

view model

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>
Lana
  • 1,024
  • 1
  • 7
  • 14
0

I solved the question without using binding, here is the c# code:

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