将WPF控件类作为模板类 [英] Have a WPF Control Class being a template class

查看:49
本文介绍了将WPF控件类作为模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以让WPF UserControl类成为具有Template类型的类?
例如

is there a way to have a WPF UserControl Class to be a class with a Template type? e.g.

public partial class MyControl : UserControl

应为:

public partial class MyControl<MyData> : UserControl

因为我总是得到 MyControl 没有引用 InitializeComponents ,它位于该类的自动生成部分中。
问题是,我无法在类的xaml部分中告诉用户控件的类型为 MyControl< MyData> 。我什至尝试 MyControl& lt; MyData& gt; ...

as I always get compile errors that MyControl than has no reference to InitializeComponents which is in the automatic generated part of the class. The problem is, that I can't tell in the xaml part of the class that the usercontrol is of type MyControl<MyData>. I even tried MyControl&lt;MyData&gt; ...

推荐答案

否,您不能在XAML中声明泛型。从 http:// social .msdn.microsoft.com / forums / zh-CN / wpf / thread / 02ca0499-80af-4c56-bb80-f1185a619a9e

No, you can't declare a generic type in XAML. From http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02ca0499-80af-4c56-bb80-f1185a619a9e:


您好,只要不使用XAML,只要
即可使用通用。但是不幸的是,如果您要使用XAML定义
控件,则
不能使用通用…

Hello, you can use generic as long as you don’t use XAML. But unfortunately, if you want to use XAML to define your control, you can’t use generic…

您可以在XAML中创建一个从通用类型继承的控件,方法是放置根标记上的 x:TypeArguments 属性,但控件本身必须是具体的。

You can create a control in XAML that inherits from a generic type by putting a x:TypeArguments attribute on the root tag, but the control itself must be concrete.

这篇关于将WPF控件类作为模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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