Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框中选定项作为字符串进行筛选_C#_Wpf_Combobox_Tostring - Fatal编程技术网

C# 组合框中选定项作为字符串进行筛选

C# 组合框中选定项作为字符串进行筛选,c#,wpf,combobox,tostring,C#,Wpf,Combobox,Tostring,我试图使用一个组合框。选择EdItem在数据网格上进行筛选,但是我在以字符串的形式访问SelectedItem时遇到问题。这就是我迄今为止所做的尝试 foreach (ComboBoxItem cItem in departmentComboBox.ItemsSource) { if (departmentComboBox.SelectedItem != null) { criteria.Add(new Predicate<EmployeeModel>

我试图使用一个组合框。选择EdItem在
数据网格
上进行筛选,但是我在以
字符串
的形式访问
SelectedItem时遇到问题。这就是我迄今为止所做的尝试

foreach (ComboBoxItem cItem in departmentComboBox.ItemsSource)
{
    if (departmentComboBox.SelectedItem != null)
    {
        criteria.Add(new Predicate<EmployeeModel>(x => x.Department == departmentComboBox.SelectedItem as string));
        break;
    }
}
x.Department
的类型为
string
。如何正确访问
SelectedItem
,以便在筛选方法中正确使用它

编辑:显示如何添加组合框项目

List<string> distinctList = Employees.Select(i => i.Department).Distinct().ToList();
distinctList.Insert(0, "Everyone");
distinctList.Sort();
departmentComboBox.ItemsSource = distinctList;
List distinctList=Employees.Select(i=>i.Department.Distinct().ToList();
distinctList.插入(0,“所有人”);
distinctList.Sort();
departmentComboBox.ItemsSource=distinctList;

您可以使用
SelectedItem
ToString()
方法

foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem)
{
if(departmentComboBox.SelectedItem!=null)
{
添加(新谓词(x=>x.Department==departmentComboBox.SelectedItem.ToString());
打破
}
}
确保组合框的项目中没有
null
值,否则可以使用以下代码:

foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem)
{
if(departmentComboBox.SelectedItem!=null)
{
添加(新谓词(x=>x.Department==“”+departmentComboBox.SelectedItem));
打破
}
}

您可以使用
SelectedItem
ToString()
方法

foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem)
{
if(departmentComboBox.SelectedItem!=null)
{
添加(新谓词(x=>x.Department==departmentComboBox.SelectedItem.ToString());
打破
}
}
确保组合框的项目中没有
null
值,否则可以使用以下代码:

foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem)
{
if(departmentComboBox.SelectedItem!=null)
{
添加(新谓词(x=>x.Department==“”+departmentComboBox.SelectedItem));
打破
}
}

您可以这样尝试:

foreach (ComboBoxItem cItem in departmentComboBox.ItemsSource){
if (departmentComboBox.SelectedItem != null)
{
    string selectedItemName = this.departmentComboBox.GetItemText(this.departmentComboBox.SelectedItem);
    criteria.Add(new Predicate<EmployeeModel>(x => x.Department.Equals(selectedItemName)));
    break;}
}
foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem){
if(departmentComboBox.SelectedItem!=null)
{
string selectedItemName=this.departmentComboBox.GetItemText(this.departmentComboBox.SelectedItem);
Add(新谓词(x=>x.Department.Equals(selectedItemName));
中断;}
}

您可以这样尝试:

foreach (ComboBoxItem cItem in departmentComboBox.ItemsSource){
if (departmentComboBox.SelectedItem != null)
{
    string selectedItemName = this.departmentComboBox.GetItemText(this.departmentComboBox.SelectedItem);
    criteria.Add(new Predicate<EmployeeModel>(x => x.Department.Equals(selectedItemName)));
    break;}
}
foreach(部门ComboBox.ItemsSource中的ComboBoxItem cItem){
if(departmentComboBox.SelectedItem!=null)
{
string selectedItemName=this.departmentComboBox.GetItemText(this.departmentComboBox.SelectedItem);
Add(新谓词(x=>x.Department.Equals(selectedItemName));
中断;}
}

您正在通过item source创建一个字符串列表来填充组合框,这很好,混淆之处在于如何访问它们。再次使用ItemSource将返回相同的字符串列表,然后尝试检查每个字符串是否与所选字符串相同。获取所选项目的更好方法是通过.SelectedItem属性。首先检查Null,您也可以放弃for循环:)

if(departmentComboBox.SelectedItem!=null)
{
Add(新谓词(x=>x.Department==departmentComboBox.SelectedItem作为字符串));
}

您正在通过item source创建一个字符串列表来填充组合框,这很好,混淆之处在于如何访问它们。再次使用ItemSource将返回相同的字符串列表,然后尝试检查每个字符串是否与所选字符串相同。获取所选项目的更好方法是通过.SelectedItem属性。首先检查Null,您也可以放弃for循环:)

if(departmentComboBox.SelectedItem!=null)
{
Add(新谓词(x=>x.Department==departmentComboBox.SelectedItem作为字符串));
}

如果SelectedValue确实是字符串,则可以使用它。但如果它实际上是您的类,则eather使用显式转换并调用所需字段(YourDepartmentClass)departmentComboBox.SelectedItem.Name例如,eather重写YourDepartmentClass.ToString方法并使用departmentComboBox.SelectedItem.ToString(覆盖为字符串是一个错误的选项)如何设置departmentComboBox.ItemSource?ItemSource用于生成ComboBoxItems,您不能使用它访问ComboBoxItemsthemselves@Cbreeze谢谢,我在下面添加了一个答案,如果它真的是字符串,您可以使用SelectedValue。但如果它实际上是您的类,则eather使用显式转换并调用所需字段(YourDepartmentClass)departmentComboBox.SelectedItem.Name例如,eather重写YourDepartmentClass.ToString方法并使用departmentComboBox.SelectedItem.ToString(覆盖为字符串是一个错误的选项)如何设置departmentComboBox.ItemSource?ItemSource用于生成ComboBoxItems,您不能使用它访问ComboBoxItemsthemselves@Cbreeze谢谢,我在下面添加了一个答案
        if (departmentComboBox.SelectedItem != null)
        {
            criteria.Add(new Predicate<EmployeeModel>(x => x.Department == departmentComboBox.SelectedItem as string));
        }