2016-10-13 47 views
0

我似乎无法使用我的组合框过滤我的列表框,尽管文本过滤器正常工作。如何解决无法将字符串转换为类别的问题?无法将字符串转换为类别错误

部分:StaffListView.xaml

StaffController sc = (StaffController)Application.Current.FindResource("staffcontroller"); 
public StaffListView() 
{ 
    InitializeComponent(); 
    StaffController sc = (StaffController)Application.Current.FindResource("staffcontroller"); 
} 

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (e.RemovedItems.Count > 0) 
    { 
     //too much going on i think it sees the() as a method because of the ToString 
     sc.FilterBy(comboBox.SelectedItem.ToString()); 
     //MessageBox.Show("Dropdown list used to select: " + e.AddedItems[0]); 
    } 
} 

StaffControler的部分:

public void FilterBy(Category currCategoryFilter) 
{ 
    var selected = from Staff s in MasterStaffListBasic 
        where currCategoryFilter == Category.All || s.StaffCategory == currCategoryFilter 
        select s; 
    ViewableStaffList.Clear(); 
    selected.ToList().ForEach(ViewableStaffList.Add); 
} 

编辑: 也只是为了澄清类别是控制器

+1

不要破坏你的帖子。 – dorukayhan

回答

1

你”定义的公共枚举已经将一个字符串传递给FilterBy方法,该方法接受一个类别作为参数。你应该通过comboBox.SelectedItem但它转换为Category这样的:

entersc.FilterBy(comboBox.SelectedItem as Category); 

根据您的编辑,你说类是公共枚举你应该通过这样的:

entersc.FilterBy((Category) comboBox.SelectedItem); 

或者,如果您仍想使用as操作:

entersc.FilterBy(comboBox.SelectedItem as Category? ?? (Category) 0); 

因为as运算符必须与引用类型或可为空类型一起使用。

相关问题