sqlite 如何使SelectionChanged事件在正确的时间运行?

ehxuflar  于 2022-11-15  发布在  SQLite
关注(0)|答案(1)|浏览(119)

在这里,我的程序显示了一个表单,您可以在其中输入有关某个主题的详细信息和该特定主题的副主题。

private void OnLoad(object sender, RoutedEventArgs e)
    {
        string connectionString = String.Format("Data Source={0}", _db);

        string queryString1 = "SELECT TopicSubject FROM tblTopic ORDER BY TopicNumber";

        // Populates subject combo box without duplicates
        SQLiteDataAdapter da = new SQLiteDataAdapter(queryString1, connectionString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (cmbSubject.Items.Contains(dt.Rows[i]["TopicSubject"]))
            { }
            else
            {
                cmbSubject.Items.Add(dt.Rows[i]["TopicSubject"]);
            }
        }

        PopulateSubtopicCombobox();
    }

    private void PopulateSubtopicCombobox()
    {
        string connectionString = String.Format("Data Source={0}", _db);
        string queryString2 = "SELECT * FROM tblTopic ORDER BY TopicNumber";

        // Populates subtopic combo box without duplicates and relating to subject
        SQLiteDataAdapter da2 = new SQLiteDataAdapter(queryString2, connectionString);
        DataTable dt2 = new DataTable();
        da2.Fill(dt2);

        for (int i = 0; i < dt2.Rows.Count; i++)
        {
            if (cmbSubtopic.Items.Contains(dt2.Rows[i]["TopicDescription"]))
            { }
            else
            {
                // If no subject has been chosen (also when first loaded)

                if (cmbSubject.Text == "")
                {
                    cmbSubtopic.Items.Add(dt2.Rows[i]["TopicDescription"]);
                }
                // If a subject has been chosen, only display subtopics related to that subject ***BROKEN***
                else if (cmbSubject.Text == Convert.ToString(dt2.Rows[i]["TopicSubject"]))
                {
                    cmbSubtopic.Items.Add(dt2.Rows[i]["TopicDescription"]);
                }
            }

        }
    }

    // When subject combo box is changed, refresh the subtopic combo box
    private void cmbSubject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        cmbSubtopic.Items.Clear();
        PopulateSubtopicCombobox();
    }

这样做的目的是让cmbSubtope只显示共享主题的副主题(因此您不能为数学主题选择历史副主题)。
但是,SelectionChanged事件似乎过早地运行了PopolateSubtopicCombobox()。当您第一次选择一个主题时,cmbSubtopy值不会改变。当您选择第二个主题时,cmb主题将更改为仅显示先前所选主题的副主题。
有什么帮助吗?

gajydyqb

gajydyqb1#

我已经重现了这个问题

public partial class MainWindow : Window
{
    public MainWindow()
    {

    }

    private class Items
    {
        public string TopicSubject;
        public string TopicDescription;
    }

    List<Items> list = new List<Items>
    {
        new Items { TopicSubject = "A", TopicDescription = "A1" },
        new Items { TopicSubject = "A", TopicDescription = "A1" },
        new Items { TopicSubject = "A", TopicDescription = "A2" },
        new Items { TopicSubject = "A", TopicDescription = "A2" },

        new Items { TopicSubject = "B", TopicDescription = "B1" },
        new Items { TopicSubject = "B", TopicDescription = "B1" },
        new Items { TopicSubject = "B", TopicDescription = "B2" },
        new Items { TopicSubject = "B", TopicDescription = "B2" },

        new Items { TopicSubject = "C", TopicDescription = "C1" },
        new Items { TopicSubject = "C", TopicDescription = "C1" },
        new Items { TopicSubject = "C", TopicDescription = "C2" },
        new Items { TopicSubject = "C", TopicDescription = "C2" },
    };

    private void OnLoad(object sender, RoutedEventArgs e)
    {
        // Populates subject combo box without duplicates
        for (int i = 0; i < list.Count; i++)
        {
            if (!cmbSubject.Items.Contains(list[i].TopicSubject))
                cmbSubject.Items.Add(list[i].TopicSubject);
        }
        PopulateSubtopicCombobox();
    }

    private void PopulateSubtopicCombobox()
    {
        // Populates subtopic combo box without duplicates and relating to subject
        for (int i = 0; i < list.Count; i++)
        {
            if (!cmbSubtopic.Items.Contains(list[i].TopicDescription))
            {
                // If no subject has been chosen (also when first loaded)
                if (cmbSubject.Text == "")
                {
                    cmbSubtopic.Items.Add(list[i].TopicDescription);
                }
                // If a subject has been chosen, only display subtopics related to that subject ***BROKEN***
                else if (cmbSubject.Text == list[i].TopicSubject)
                {
                    cmbSubtopic.Items.Add(list[i].TopicDescription);
                }
            }
        }
    }

    private void cmbSubject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        cmbSubtopic.Items.Clear();
        PopulateSubtopicCombobox();
    }
}

问题是cmbSubject.Text尚未更新
使用SelectionChangedEventArgs e而不是cmbSubject.Text

private void OnLoad(object sender, RoutedEventArgs e)
{
    // Populates subject combo box without duplicates
    for (int i = 0; i < list.Count; i++)
    {
        if (!cmbSubject.Items.Contains(list[i].TopicSubject))
            cmbSubject.Items.Add(list[i].TopicSubject);
    }
    PopulateSubtopicCombobox("");
}

private void PopulateSubtopicCombobox(string useThisText)
{
    // Populates subtopic combo box without duplicates and relating to subject
    for (int i = 0; i < list.Count; i++)
    {
        if (!cmbSubtopic.Items.Contains(list[i].TopicDescription))
        {
            // If no subject has been chosen (also when first loaded)
            if (useThisText == "") // change this!!!!!!!!!!
            {
                //cmbSubtopic.Items.Add(list[i].TopicDescription);
            }
            // If a subject has been chosen, only display subtopics related to that subject ***BROKEN***
            else if (useThisText == list[i].TopicSubject) // change this!!!!!!!!!!
            {
                cmbSubtopic.Items.Add(list[i].TopicDescription);
            }
        }
    }
}

private void cmbSubject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var useThisText = e.AddedItems[0].ToString();

    cmbSubtopic.Items.Clear();
    PopulateSubtopicCombobox(useThisText);
}

MainWindow.xmal

<Window x:Class="WpfApp1.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Height="600" Width="800" Loaded="OnLoad">
    <StackPanel>
        <ComboBox x:Name="cmbSubject" SelectionChanged="cmbSubject_SelectionChanged"/>
        <ComboBox x:Name="cmbSubtopic"/>
    </StackPanel>
</Window>

相关问题