我對WPF相當陌生,我正在和Bindings玩耍。我設法將綁定設置爲List
以便顯示例如網格中的人員列表。我現在想要的是在綁定上設置一個條件,並且只從網格中選擇滿足這種條件的人。我至今是:如何設置綁定到列表的選擇條件?
// 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>
我現在想什麼,是隻在列表中包括Person
情況下,誰是餓了。我知道一個辦法做到這一點的代碼,例如添加新的屬性:
public List<Person> HungryPeople
{
get
{
List<Person> hungryPeople = new List<Person>();
foreach (Person person in People)
if (person.isHungry)
hungryPeople.Add(person);
return hungryPeople;
}
}
,然後更改綁定到HungryPeople
代替。不過,我並不認爲這是一個很好的選擇,因爲它涉及到製造額外的公共財產,這可能不合意。有沒有辦法在XAML代碼中實現這一切?
如果您想按需排序和過濾,您將需要實現自定義[集合視圖](http://msdn.microsoft.com/zh-cn/library/system.componentmodel.icollectionview.aspx)。 – Will