c# - style - ComboBox SelectionChanged事件觸發較晚



wpf dropdownlist (1)

在我的WPF應用程序中,我有一個ComboBox,用於為AutoCompletebox選擇ItemFilter。 這裡是代碼:

XAML

<ComboBox 
     Name="SearchFilter" 
     HorizontalAlignment="Right" 
     MinWidth="75" Margin="0,3,0,3" 
     SelectionChanged="SearchFilter_SelectionChanged">
          <ComboBoxItem>Full-Time</ComboBoxItem>
          <ComboBoxItem>Part-Time</ComboBoxItem>
          <ComboBoxItem>Retired</ComboBoxItem>
          <ComboBoxItem>Stockholder</ComboBoxItem>
          <ComboBoxItem>Terminated</ComboBoxItem>
          <ComboBoxItem>None</ComboBoxItem>
</ComboBox>

C#

private void SearchFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (SearchFilter.SelectedItem != null)
        {
            if (SearchFilter.Text == "Full-Time")
            {
                EmployeeAutoBox.ItemFilter = PersonFilter_Full;
            }
            else if (SearchFilter.Text == "Part-Time")
            {
                EmployeeAutoBox.ItemFilter = PersonFilter_Part;
            }
            else if (SearchFilter.Text == "Retired")
            {
                EmployeeAutoBox.ItemFilter = PersonFilter_Ret;
            }
            else if (SearchFilter.Text == "Stockholder")
            {
                EmployeeAutoBox.ItemFilter = PersonFilter_Stock;
            }
            else if (SearchFilter.Text == "Terminated")
            {
                EmployeeAutoBox.ItemFilter = PersonFilter_Term;
            }
            else
            {
                EmployeeAutoBox.ItemFilter = PersonFilter;
            }
        }
    }

出於某種原因,我改變選擇之後,正在應用過濾器。 例如,我將組合框設置為“全職”,列表篩選器沒有被應用。 然後,我將組合框設置為“兼職,全時間過濾器正在應用,然後我將組合框設置為”退休“,部分時間過濾器正在應用等等等等...我用一個組合框以前也是類似的事情,它通常是基於盒子裡的東西,而不是盒子裡的東西,我在這裡錯過了什麼?


TextSearchFilter的唯一屬性,它不會在你的SelectionChanged處理程序中被更新(不要問我為什麼)。

SelectedItem將是好的, SelectedValue將是好的(在你的情況下,兩者都將是選定的ComboBoxItem - 不是一個偉大的方式來使用WPF,但我不是你的牧師),和SelectedIndex

我們將對XAML進行一個小改動(見下文),以便從SelectedValue獲取所選字符串。

private void SearchFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //  Not sure there's any reason for this null check. 
    if (SearchFilter.SelectedValue != null)
    {
        var filter = SearchFilter.SelectedValue as String;

        switch (filter)
        {
            case "Full-Time":
                EmployeeAutoBox.ItemFilter = PersonFilter_Full;
                break;
            case "Part-Time":
                EmployeeAutoBox.ItemFilter = PersonFilter_Part;
                break;
            case "Retired":
                EmployeeAutoBox.ItemFilter = PersonFilter_Ret;
                break;
            case "Stockholder":
                EmployeeAutoBox.ItemFilter = PersonFilter_Stock;
                break;
            case "Terminated":
                EmployeeAutoBox.ItemFilter = PersonFilter_Term;
                break;
            default:
                EmployeeAutoBox.ItemFilter = PersonFilter;
                break;
        }
    }
}

XAML:除了縮進之外,唯一的改變是添加SelectedValuePath="Content"屬性。 所做的是,當選擇發生變化時(和事件發生之前),ComboBox現在將查看SelectedItem中的對象,無論它是什麼,並查找名為“Content”的屬性。 如果它發現,它將使用SelectedItem的ContentValue的Content屬性的值。 你給的這些內容是字符串:“兼職”等等

<ComboBox 
    Name="SearchFilter" 
    SelectedValuePath="Content"
    HorizontalAlignment="Right" 
    MinWidth="75" 
    Margin="0,3,0,3" 
    SelectionChanged="SearchFilter_SelectionChanged"
    >
    <ComboBoxItem Tag="Full-Time">Full-Time</ComboBoxItem>
    <ComboBoxItem>Part-Time</ComboBoxItem>
    <ComboBoxItem>Retired</ComboBoxItem>
    <ComboBoxItem>Stockholder</ComboBoxItem>
    <ComboBoxItem>Terminated</ComboBoxItem>
    <ComboBoxItem>None</ComboBoxItem>
</ComboBox>




combobox