ComboBox SelectionChanged事件触发得晚 [英] ComboBox SelectionChanged event fires late

查看:126
本文介绍了ComboBox SelectionChanged事件触发得晚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我有一个ComboBox,用于为AutoCompletebox选择ItemFilter。以下是代码:

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

由于某种原因,过滤器我更改选择后正在应用。例如,我将ComboBox设置为 Full-Time,则未应用列表过滤器。然后,将ComboBox设置为兼职,将应用全职过滤器。然后,将ComboBox设置为已退休,将应用兼职过滤器。等等,等等。之前类似的事情,它通常是根据包装盒中的当前内容而不是包装盒中的内容进行工作。我在这里缺少什么?

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?

推荐答案

文本几乎是 SearchFilter 的唯一属性,该属性不会在SelectionChanged处理程序中更新(不要问我为什么不这样做。)

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 会很好, SelectedValue 会很好(在您的情况下,两个都将是选定的 ComboBoxItem -不是使用WPF的好方法,但是我不是您

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.

我们将对XAML进行一些小的更改(请参见下文),可以从 SelectedValue 获取选定的字符串。

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:唯一的改变从缩进中添加 SelectedValuePath = Content 属性。就是这样,当选择更改时(在引发事件之前),ComboBox现在将查看 SelectedItem 中的对象,无论它是什么,并寻找一个名为内容的属性。如果找到,它将使用 SelectedItem Content 属性的值作为 SelectedValue 。您为这些内容提供的字符串是:兼职等。

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>

这篇关于ComboBox SelectionChanged事件触发得晚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆