Select 选择组时的组合框项目

Select Combobox items when selecting a group

我的 .xaml

中有一个组合框
    <ComboBox Name="Group" Margin="50,71,330,618" Grid.Column="1"/>

还有我的另一个组合框。xaml

<ComboBox Name="Sort" Margin="20,71,0,618"/>

当我从 'Liquid' 之类的组合框中选择一个项目时,我只想查看 Table Sort 中具有 GroupName 液体.

我的 FK/PK 在数据库中是正确的,但我不知道如何将它放入我的 WPF 中。

我正在使用 Visual Studio 2019,WPF 中的 C#

在我的 .cs 文件中,我有以下查询,因此它将始终显示组合框中的项目

  public AddStock()
    {
        InitializeComponent();
        bindcomboboxgroup();
    }

        private void bindcomboboxgroup()
    {

        try
        {
            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\"local";
            string SelectQuery = "SELECT Name From [Group]";
            con.Open();
            SqlCommand cmd = new SqlCommand(SelectQuery, con);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Groep.Items.Add(reader.GetString("Name"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occurred:\r\n" + ex.Message);
        }
    }

以下解决方案似乎对您来说已经足够了:

我使用以下值创建了两个 table(分组 table 和排序 table):

注意:如果需要使用Application.StartupPath,请添加System.Windows.Forms引用。

using System.Windows;
using System.Windows.Controls;
namespace WpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        System.Data.SqlClient.SqlConnection SQLConnect = new System.Data.SqlClient.SqlConnection("Data Source = .\;Integrated Security=True;AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + @"\LocalDB.mdf;");
        public MainWindow()
        {
            InitializeComponent();
            System.Data.SqlClient.SqlCommand CMD = new System.Data.SqlClient.SqlCommand("Select * from GroupTable", SQLConnect);
            SQLConnect.Open();
            System.Data.SqlClient.SqlDataReader SDR = CMD.ExecuteReader();
            GroupComboBox.Items.Clear();
            while (SDR.Read())
            {
                for (int i = 0; i < SDR.FieldCount; i++)
                {
                    GroupComboBox.Items.Add(SDR[i].ToString());
                }
            }
            SQLConnect.Close();
        }

        private void Group_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Data.SqlClient.SqlCommand CMD;
            switch (GroupComboBox.SelectedItem.ToString())
            {
                case "Gas":
                    CMD = new System.Data.SqlClient.SqlCommand("Select G from SortTable", SQLConnect);
                    break;
                case "Liquid":
                    CMD = new System.Data.SqlClient.SqlCommand("Select L from SortTable", SQLConnect);
                    break;
                case "Plasma":
                    CMD = new System.Data.SqlClient.SqlCommand("Select P from SortTable", SQLConnect);
                    break;
                default:
                    CMD = new System.Data.SqlClient.SqlCommand("Select S from SortTable", SQLConnect);
                    break;
            }
            SQLConnect.Open();
            System.Data.SqlClient.SqlDataReader SDR = CMD.ExecuteReader();
            SortComboBox.Items.Clear();
            while(SDR.Read())
            {
                SortComboBox.Items.Add(SDR[0].ToString());    
            }
            SQLConnect.Close();
        }
    }
}

输出:

测试时间:

Visual Studio 2017,.NET Framework 4.5.2,WPF,SQL 服务器 12.0.6024.0

谢谢