WPF和ObservableCollection< T> [英] WPF and ObservableCollection<T>

查看:74
本文介绍了WPF和ObservableCollection< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要显示的ObservableCollection<IRuleCondition>-我要显示的2个不同类使用IRuleCondition界面,一个RuleCondition仅显示一个规则条件(诸如优先级,要检查的属性的信息)等等)和一个RuleConditionGroup,它可以包含2个或多个RuleConditions,并且按照可以匹配任何条件或全部匹配的方式进行分组.

I have an ObservableCollection<IRuleCondition> that I want to display - the IRuleCondition interface is used by 2 different classes I want to display, a RuleCondition that simply displays one rule condition (info such as priority, property to check and so on), and a RuleConditionGroup, that can contain 2 or more RuleConditions, grouped in such a way that any of the conditions could match, or all etc.

在XAML中,我想知道是否有一种方法可以显示不同的ListView.ItemTemplate,具体取决于它在ObservableCollection<IRuleCondition>中遇到的类型是什么?还是我需要实现两个不同的ObservableCollection?

In the XAML I was wondering is there a way to display a different ListView.ItemTemplate depending on what the type is that it encounters in the ObservableCollection<IRuleCondition>? Or would I need to implement two different ObservableCollections?

推荐答案

这里是一个简单的示例

这是定义对象的方式

public interface Person
{
    string Name { get; set; }
}

public class Manager : Person
{
    public string Name { get; set; }
}

public class Employee : Person
{
    public string Name { get; set; }
    public string ManagerName { get;set;}
}

这是背后的MainWindow代码

This is the MainWindow code behind

public partial class MainWindow : Window
    {
        ObservableCollection<Person> mPeople = new ObservableCollection<Person>();

        public ObservableCollection<Person> People
        {
            get
            {
                return mPeople;
            }
        }

        public MainWindow()
        {
            DataContext = this;
            mPeople.Add( new Employee{ Name = "x" , ManagerName = "foo"});
            mPeople.Add( new Manager { Name = "y"});

            InitializeComponent();
        }
    }

这是MainWindow XAML

This is the MainWindow XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <DataTemplate  DataType="{x:Type my:Employee}">
            <StackPanel Background="Green" Width="300">
                <TextBlock Text="{Binding Name}" />
                <TextBlock Text="{Binding ManagerName}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate  DataType="{x:Type my:Manager}">
            <StackPanel Background="Red"
                        Width="300">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding People}"></ListBox>
    </Grid>
</Window>

如您所见,有两个数据模板,一个用于Manager,一个用于Employee

As you can see there are two datatemplates one for Manager and one for Employee

这就是糟糕的输出的样子.请注意,与经理相比,该员工显示的绿色和红色背景以及多余的字段

And this is how the crappy output looks like. Notice the green and red background and extra field displayed for the Employee compared to the manager

这篇关于WPF和ObservableCollection&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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