Linq查询作为绑定源

实现效果:

clipboard.png

clipboard.png
关键词:

  1. Linq根据条件在集合选择出匹配项作为绑定源DataContext

xaml:

<StackPanel>
    <TextBlock Margin="10,0,0,0">Choose a Priority:</TextBlock>
    <ListBox SelectionChanged="ListBox_SelectionChanged"
             SelectedIndex="0" Margin="10,0,10,0" >
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
        <ListBoxItem>3</ListBoxItem>
    </ListBox>
    <ListBox Width="400" Margin="10" Name="myListBox"
             HorizontalContentAlignment="Stretch"
             ItemsSource="{Binding}"
             ItemTemplate="{StaticResource MyTaskTemplate}"/>
</StackPanel>

以上第二个ListBox进行绑定,但未具体指定绑定源,由第一个ListBox的选定项作为筛选参数后台代码提供。

private readonly Tasks tasks = new Tasks();

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var pri = int.Parse(((sender as ListBox).SelectedItem as ListBoxItem).Content.ToString());

    DataContext = from task in tasks
        where task.Priority == pri
        select task;
}

扩展:

  1. 以上。

李志玮
22 声望34 粉丝

求索~~


引用和评论

0 条评论