ComboBox SelectionChanged事件触发

时间:2017-04-19 19:56:51

标签: c# wpf xaml combobox

在我的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;
            }
        }
    }

出于某种原因,我在更改选择后更改了过滤器。例如,我将ComboBox设置为“全时”,未应用列表过滤器。然后我将ComboBox设置为“Part-Time,正在应用全时过滤器。然后我将ComboBox设置为”Retired“,正在应用Part Time过滤器。等等...我使用了ComboBox for以前类似的东西,通常根据盒子里的东西而不是盒子里面的东西。我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

Text只是SearchFilter中唯一不会在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 Content SelectedValue属性的值<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> 。你给出的内容是字符串:“兼职”等等。那么

Section "Device"
  Identifier "display"
  Driver "fbdev"
  Option "fbdev" "/dev/fb1"
EndSection