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



在此图像上

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

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

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>

我在不使用绑定的情况下解决了这个问题,下面是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相同。

最新更新