ComboBox SelectionChanged 事件延迟触发 [英] ComboBox SelectionChanged event fires late

查看:55
本文介绍了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 设置为全职",未应用列表过滤器.然后我将 ComboBox 设置为兼职,正在应用全职过滤器.然后我将 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?

推荐答案

Text 几乎是 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,但我不是你的牧师)和 SelectedIndex.

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" 属性.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 "内容".如果找到,它将使用 SelectedItemContent 属性的值作为 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天全站免登陆