无法将字符串转换为类别错误
本文关键字:错误 转换 字符串 | 更新日期: 2023-09-27 18:13:03
我似乎不能过滤我的列表框与我的组合框,虽然文本过滤器工作得很好。如何解决无法将字符串转换为类别的问题?
部分代码: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]);
}
}
部分的staffcontroller:
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);
}
编辑:还要澄清一下Category是控制器中定义的公共enum
您已经向FilterBy
方法传递了一个字符串,该方法接受类别作为参数。您应该传递comboBox.SelectedItem
,但将其强制转换为Category
,如下所示:
entersc.FilterBy(comboBox.SelectedItem as Category);
根据你的编辑,你说Category是一个公共enum你应该像这样传递它:
entersc.FilterBy((Category) comboBox.SelectedItem);
或者如果您仍然想使用as
操作符:
entersc.FilterBy(comboBox.SelectedItem as Category? ?? (Category) 0);
因为as
运算符必须与引用类型或可空类型一起使用