Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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_Binding - Fatal编程技术网

C# 如何在绑定到列表时设置选择条件?

C# 如何在绑定到列表时设置选择条件?,c#,wpf,binding,C#,Wpf,Binding,我是WPF的新手,我正在玩绑定。我设法将绑定设置为列表,以便显示网格中的人员列表。我现在想要的是在绑定上设置一个条件,并且只从网格中选择满足这个条件的人。到目前为止,我得到的是: // In MyGridView.xaml.cs public class Person { public string name; public bool isHungry; } public partial class MyGridView: UserControl { List<

我是WPF的新手,我正在玩绑定。我设法将绑定设置为
列表
,以便显示网格中的人员列表。我现在想要的是在绑定上设置一个条件,并且只从网格中选择满足这个条件的人。到目前为止,我得到的是:

// In MyGridView.xaml.cs
public class Person
{ 
    public string name;
    public bool isHungry;
}

public partial class MyGridView: UserControl
{
    List<Person> m_list;
    public List<Person> People { get {return m_list;} set { m_list = value; } }

    public MyGridView() { InitializeComponent(); }
}

// In MyGridView.xaml

<UserControl x:Class="Project.MyGridView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>    
         <DataGrid Name="m_myGrid" ItemsSource="{Binding People}" />
    </Grid>
</UserControl>

然后将绑定改为
HungryPeople
。然而,我不认为这是一个很好的选择,因为它涉及到额外的公共财产,这可能是不可取的。有没有办法在XAML代码中实现这一切?

您不需要多个属性,只需为您的
Person
类创建一个
ObservableCollection
,并绑定到
数据网格的
ItemsSource

 public ObservableCollection<Person> FilterPersons{get;set;}

 <DataGrid Name="m_myGrid" ItemsSource="{Binding FilterPersons}" />
public observeCollection过滤器persons{get;set;}
使用视图构造函数中的
人员
主列表初始化此集合


在每个筛选器(例如匈牙利、口渴等)上,只需从
FilterPersons
中添加/删除
Person
,您的
DataGrid
将相应更新。

您不需要多个属性,只需为您的
Person
类创建一个
ObservableCollection
,并绑定到
DataGrid的
ItemsSource

 public ObservableCollection<Person> FilterPersons{get;set;}

 <DataGrid Name="m_myGrid" ItemsSource="{Binding FilterPersons}" />
public observeCollection过滤器persons{get;set;}
使用视图构造函数中的
人员
主列表初始化此集合


在每个过滤器上(例如,匈牙利、口渴等等),只需从
FilterPersons
添加/删除
Person
,您的
DataGrid
就会相应地更新。

您可以在绑定中使用转换器,在那里您可以根据需要过滤列表,并返回过滤后的列表

<DataGrid Name="m_myGrid" ItemsSource="{Binding People, Converter=myConverter}" />

而转换器—

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<Person> hungryPeople = new List<Person>();
        foreach (Person person in value as List<Person>)
            if (person.isHungry)
                hungryPeople.Add(person);
        return hungryPeople;
    }
}
公共类MyConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
List hungryPeople=新列表();
foreach(值中的人员作为列表)
if(person.isHungry)
饥饿者。添加(人);
回归饥饿;
}
}

您可以在绑定中使用转换器,在那里您可以根据需要过滤列表,并返回过滤后的列表

<DataGrid Name="m_myGrid" ItemsSource="{Binding People, Converter=myConverter}" />

而转换器—

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<Person> hungryPeople = new List<Person>();
        foreach (Person person in value as List<Person>)
            if (person.isHungry)
                hungryPeople.Add(person);
        return hungryPeople;
    }
}
公共类MyConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
List hungryPeople=新列表();
foreach(值中的人员作为列表)
if(person.isHungry)
饥饿者。添加(人);
回归饥饿;
}
}
使用带过滤器的过滤器:

约束力:

<UserControl x:Class="Project.MyGridView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
    <CollectionViewSource x:key="PeopleView" Source="{Binding People} Filter="ShowOnlyHungryPeople" />
</UserControl.Resources>
    <Grid>    
         <DataGrid Name="m_myGrid" ItemsSource="{Binding Source={StaticResource PeopleView}}" />
    </Grid>
</UserControl>
与过滤器一起使用:

约束力:

<UserControl x:Class="Project.MyGridView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
    <CollectionViewSource x:key="PeopleView" Source="{Binding People} Filter="ShowOnlyHungryPeople" />
</UserControl.Resources>
    <Grid>    
         <DataGrid Name="m_myGrid" ItemsSource="{Binding Source={StaticResource PeopleView}}" />
    </Grid>
</UserControl>

如果您想按需排序和筛选,您将需要实现自定义。如果您想按需排序和筛选,您将需要实现自定义。这是一个极好的解决方案,非常有效!不幸的是,
MyGridView
DataContext
一旦改变,整个过程就崩溃了。如何将
过滤器
显式绑定到
MyGridView.ShowOnlyHungryPeople
?这是一个极好的解决方案,非常有效!不幸的是,
MyGridView
DataContext
一旦改变,整个过程就崩溃了。如何将
过滤器
显式绑定到
MyGridView.ShowOnlyHungryPeople