将ObservableCollection筛选到多个列表框 [英] Filtering an ObservableCollection to multiple list boxes

查看:153
本文介绍了将ObservableCollection筛选到多个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我目前有一个ObservableCollection,它已填充到我的ViewModel构造函数中. ObservableCollection包含一个具有两个属性(两个字符串)的自定义对象.

In my project I currently have an ObservableCollection that is populated inside of my ViewModel constructor. This ObservableCollection holds a custom object which has two properties (both strings).

当前,XAML/View对应项包含两个单独的列表框,这两个列表框都绑定到DataTemplate,后者选择要在ListBox中显示为条目的属性.在这种情况下,它会显示"propertyOne".

Currently, the XAML/View counterpart holds two separate list boxes, both of which are bonded to a DataTemplate that selects which property to display as an entry in the ListBox. In this case it displays 'propertyOne'.

是否可能有一个DataTemplate可以根据'propertyTwo'的内容选择每个ListBox条目的位置?

Is it possible to have a DataTemplate that can select where each ListBox-item goes to depending on the content of 'propertyTwo'?

我已经研究了与我的情况类似的示例,该示例使用了CollectionViewSource,但是我不太确定如何将其实现到我的项目中,因为我对使用WPF和遵循MVVM结构是相当陌生的.这会涉及在View背后的代码中创建一个过滤器事件吗?

I have looked into examples similar to my situation, which used CollectionViewSource but I am not too sure as to how I would implement this into my project, as I am fairly new to using WPF and following the MVVM structure. Would this involve creating a filter event in the code-behind the View?

下面列出的是我的代码段,我认为这些代码段有助于理解我的问题.解决该问题的任何帮助将不胜感激.

Listed below are snippets of my code that I think would be useful in understanding my question. Any help on solving this would greatly be appreciated.

<Window.Resources>
    <DataTemplate x:Key="ListBoxTemplate">
        <StackPanel>
            <TextBlock Text="{Binding Path=propertyOne}" />
        </StackPanel>
    </DataTemplate> 
</Window.Resources>

<ListBox x:Name="ListBoxOne"
         Height="Auto"
         Width="Auto"
         ItemsSource="{Binding TestCollection}"
         ItemTemplate="{StaticResource ListBoxTemplate}" />

<ListBox x:Name="ListBoxTwo"
         Height="Auto"
         Width="Auto"
         ItemsSource="{Binding TestCollection}"
         ItemTemplate="{StaticResource ListBoxTemplate}" />     

ViewModel

public class ViewModel
{
    public ObservableCollection<Item> TestCollection { get; set; }

    public ViewModel()
    {
        //populates the collection from an XML file
        //with propertyOne & propertyTwo for each item

        TestCollection = CustomObjectClass.DeserializeToColl<Item>("path");
    }
}

CustomObjectClass

public class CustomObjectClass
{
    public string propertyOne { get; set; }
    public string propertyTwo { get; set; }
}

推荐答案

<DataTemplate x:Key="ListBoxTemplate">
        <StackPanel>
            <StackPanel.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=propertyTwo}" Value="read">
                            <Setter Property="StackPanel.Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            <TextBlock Text="{Binding Path=propertyOne}" />
        </StackPanel>
    </DataTemplate>

这篇关于将ObservableCollection筛选到多个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆