一种类型中的两个通用列表

时间:2020-12-24 20:14:50

I would like to style generic variables with XAML. For that I need {x:Type myTypes:theType}.

我想用XAML设置泛型变量的样式。为此,我需要{x:输入myTypes:theType}。

So I have:

所以我有:

public class BaseClass
{
    //
}

public class ClassA : BaseClass
{
    //
}

public class ClassB : BaseClass
{
    //
}

public class ListA : List<ClassA>
{
    //
}

public class ListB : List<ClassB>
{
    //
}

Is it possible that I create a class that can be eather ListA or ListB?

是否有可能创建一个可以收集ListA或ListB的类?

I would need to use it for a DataTemplate with TargetType and I would like to make just one Template for ListA and ListB without a TemplateSelector.

我需要将它用于带有TargetType的DataTemplate,我想在没有TemplateSelector的情况下为ListA和ListB创建一个模板。

Thank you!

2 个解决方案

#1


What do you mean? Something like this?

你什么意思?像这样的东西?

List<int> listA = new List<int>() { 1, 2, 3 };
List<string> listB = new List<string>() { "A", "B", "C" };

ICollection ListC = listA;

ListC = listB;

#2


No, but you could implement common functionality as an interface.

不,但您可以将通用功能实现为接口。

In this example, I modified your ListA and ListB to expose a common interface. I'll admit that I do not know WPF that well, but this would allow you to use common methods in either a ListA or ListB instance.

在此示例中,我修改了ListA和ListB以显示公共接口。我承认我不太了解WPF,但这将允许您在ListA或ListB实例中使用常用方法。

public class BaseClass
{
    public string BaseClassFoo { get; set; }
}

public class ClassA : BaseClass
{
    public string ClassAFoo { get; set; }
}

public class ClassB : BaseClass
{
    public string ClassBFoo { get; set; }
}

public interface IMyList {
    void CommonOperation();
}

public class ListA : List<ClassA>, IMyList
{
    public void CommonOperation() {
        Console.WriteLine(this[0].ClassAFoo);
    }
}

public class ListB : List<ClassB>, IMyList
{
    //
    public void CommonOperation() {
        Console.WriteLine(this[0].ClassBFoo);
    }
}

class Program {
    private static void Main(string[] args) {
        IMyList a = new ListA { new ClassA { ClassAFoo = "I'm an A" } };
        IMyList b = new ListB { new ClassB { ClassBFoo = "I'm a B" } };

        a.CommonOperation();
        b.CommonOperation();
    }
}

Output:

I'm an A
I'm a B

#1


What do you mean? Something like this?

你什么意思?像这样的东西?

List<int> listA = new List<int>() { 1, 2, 3 };
List<string> listB = new List<string>() { "A", "B", "C" };

ICollection ListC = listA;

ListC = listB;

#2


No, but you could implement common functionality as an interface.

不,但您可以将通用功能实现为接口。

In this example, I modified your ListA and ListB to expose a common interface. I'll admit that I do not know WPF that well, but this would allow you to use common methods in either a ListA or ListB instance.

在此示例中,我修改了ListA和ListB以显示公共接口。我承认我不太了解WPF,但这将允许您在ListA或ListB实例中使用常用方法。

public class BaseClass
{
    public string BaseClassFoo { get; set; }
}

public class ClassA : BaseClass
{
    public string ClassAFoo { get; set; }
}

public class ClassB : BaseClass
{
    public string ClassBFoo { get; set; }
}

public interface IMyList {
    void CommonOperation();
}

public class ListA : List<ClassA>, IMyList
{
    public void CommonOperation() {
        Console.WriteLine(this[0].ClassAFoo);
    }
}

public class ListB : List<ClassB>, IMyList
{
    //
    public void CommonOperation() {
        Console.WriteLine(this[0].ClassBFoo);
    }
}

class Program {
    private static void Main(string[] args) {
        IMyList a = new ListA { new ClassA { ClassAFoo = "I'm an A" } };
        IMyList b = new ListB { new ClassB { ClassBFoo = "I'm a B" } };

        a.CommonOperation();
        b.CommonOperation();
    }
}

Output:

I'm an A
I'm a B