简体   繁体   中英

ComboBox SelectionChanged event fires late

In my WPF application, I have a ComboBox that I am using to select an ItemFilter for an AutoCompletebox. Here is the code:

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;
            }
        }
    }

For some reason, the filter is being applied after I changed the selection is changed. For example, I set the ComboBox to "Full-Time", the list filter is not being applied. Then I set the ComboBox to "Part-Time, the Full-Time filter is being applied. Then I set the ComboBox to "Retired", the Part Time filter is being applied. Etc, etc... I have used a ComboBox for similar things before and it usually works based on what is currently in the box, not what was in the box. What am I missing here?

Text is just about the only property of SearchFilter that won't have been updated in your SelectionChanged handler (don't ask me why not).

SelectedItem will be good, SelectedValue will be good (in your case, both will be the selected ComboBoxItem -- not a great way to use WPF, but I'm not your priest), and SelectedIndex .

We'll make one small change to the XAML (see below) so we can get the selected string from 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: The only change aside from indenting is adding the SelectedValuePath="Content" attribute. What that does is, when the selection changes (and before the event is raised), the ComboBox will now look at the object in SelectedItem , whatever it may be, and look for a property on it named "Content". If it finds that, it'll use the value of the SelectedItem 's Content property for SelectedValue . The content you're giving these is the strings: "Part-Time", etc. So then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM