2017-04-19 140 views
0

在我的WPF应用程序中,我有一个ComboBox,用于为AutoCompletebox选择ItemFilter。下面是代码:ComboBox SelectionChanged事件火灾晚

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设置为“Part-Time,全时间过滤器正在应用,然后我将ComboBox设置为”Retired“,Part Time过滤器正在应用等等等等......我已经使用了ComboBox for类似的东西之前,它通常基于目前在框中的东西,而不是在框中是什么。我在这里缺少什么?

+0

什么在这个SelectionChanged处理程序中的'e.AddedItems'? 'SearchFilter.SelectedItem'和'SearchFilter.SelectedValue'本应该已经更新了 –

回答

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”的属性。如果它发现,它将使用SelectedItemContent属性的值为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> 
+0

哇,反应很快。您上面的评论,提醒我使用SelectedItem代替。我对WPF相当陌生,并不熟悉MVVM。你为什么说它不是使用WPF的好方法?我总是乐于采取更好的方式来做事,即使它只是一个概念或我可以查找的东西。 – Pants

+0

@Pants例如,更正确的方法是通过将其ItemsSource绑定到viewmodel的集合属性来填充ComboBox,然后'SelectedItem'将是集合中的一个项目。在这种情况下,您可以像我在更新中那样使用'SelectedValuePath =“WhateverProperty”',然后将'SelectedValue'绑定到另一个viewmodel属性。把你的整个项目变成一个MVVM的东西在这一点上可能会被禁止;我不想对此过于吝啬。 –

+0

作为一个工作项目,他们想要一些快速而肮脏的工作,可能有点过分,但没有理由不改善我前进的方式,如果时间允许,最后可能会收拾。我会给你一些想法。干杯! – Pants