使用linq变化一下Source,换成是Linq查询出的结果就行
XMAL代码
<Window x:Class="使用LInq作为Binding的源.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Background="LightBlue">
<ListView x:Name="list" Height="130" Margin="5">
<ListView.View>
<GridView>
<GridViewColumn Header="名称" Width="60" DisplayMemberBinding="{Binding Path=Name}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>
CS代码
namespace 使用LInq作为Binding的源
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<User> oc = new ObservableCollection<User>()
{
new User(){Name="张三"},
new User(){Name="李氏"},
new User(){Name="张五"},
new User(){Name="李广"},
new User(){Name="张笑话"},
new User(){Name="李小天"},
};
//查出所有姓李的
this.list.ItemsSource = from o in oc where o.Name.StartsWith("李") select o;
}
}
}
User类
namespace 使用LInq作为Binding的源
{
class User
{
public string Name { get; set; }
}
}