用户在 XAML 中定义的顶级控件

发布于 2024-08-26 07:14:59 字数 423 浏览 10 评论 0原文

普通的 UserControl 在 XAML 中看起来像这样:

<UserControl x:Class="mynamespace.foo" ...namespaces...>
<!-- content -->
</UserControl>

我希望能够定义我自己的顶级对象,大致如下:

<MyControl x:Class="mynamespace.mycontrol" ...namespaces...>
<!-- content -->
</UserControl>

其中 MyControl 派生自 UserControl 本身。

当然,编译器会抱怨找不到“MyControl”。有办法解决这个问题吗?

A normal UserControl looks like this in XAML:

<UserControl x:Class="mynamespace.foo" ...namespaces...>
<!-- content -->
</UserControl>

I'd like to be able to define my own top level object, along the lines of:

<MyControl x:Class="mynamespace.mycontrol" ...namespaces...>
<!-- content -->
</UserControl>

Where MyControl derives from a UserControl itself.

Of course the compiler complains about "MyControl" not being found. Is there a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-09-02 07:14:59

根标签是基类。这就是为什么默认Window1的根目录是Window。使用菜单选项“添加”> UserContol... 实际上是为 UserContol 创建一个子类。

如果您有一些通用元素并想要一个控件基类,您可以使用基类作为根标记。您无法从任何具有 xaml 定义的可视化树的类派生您的类,但您的基类可以从 UserConrtol 派生。

首先定义您的基类:

public class MyControlBase : UserControl
{
    // ...
}

然后创建您的特定子类:(

您可以从自动创建的 UserControl1 开始并从那里更改它)

public partial class MyControl1 : MyControlBase
{
    public MyControl1()
    {
        InitializeComponent();
    }
}

然后将 Xaml 端更改为如下所示:

<MyNamespace:MyControlBase
    x:Class="MyNamespace.MyControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyNamespace="clr-namespace:MyNamespace">

这是从内置派生自定义控件的好方法在其他用户控件中。如果可以的话,通常建议仅使用基本的 UserConrtols,并且仅在必要时才创建自​​定义控件。

祝你好运,

The root tag is the base class. That's why the root of the default Window1 is Window. Using the menu option Add > UserContol... is in fact creating a sub-class for UserContol.

If you have some common elements and want a control base class you can use the base class as the root tag. You can't derive your class from any class that has a xaml defined visual tree, but your base class can derive from UserConrtol.

First define your base class:

public class MyControlBase : UserControl
{
    // ...
}

Then create your specific child class:

(You can start with the automatically created UserControl1 and change it from there)

public partial class MyControl1 : MyControlBase
{
    public MyControl1()
    {
        InitializeComponent();
    }
}

Then change the Xaml side to look like this:

<MyNamespace:MyControlBase
    x:Class="MyNamespace.MyControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyNamespace="clr-namespace:MyNamespace">

This is a great way to make custom controls derived from built in ones other that UserControl. It is typically recommended to just use basic UserConrtols if you can and make a custom control only if you have to.

good luck,

溇涏 2024-09-02 07:14:59

在 XAML 中定义命名空间,然后使用控件名称作为标记:

<Window ...
    xmlns:my="..." />

    <my:mycontrol ... />

</Window>

Define your namespace in the XAML and then use your control name as the tag:

<Window ...
    xmlns:my="..." />

    <my:mycontrol ... />

</Window>
七分※倦醒 2024-09-02 07:14:59

不。XAML 声明了 MyControl 的视觉含义,就像隐藏代码定义了 MyControl 的行为含义一样。根据 MyControl 定义 MyControl 的视觉效果实际上没有意义:这相当于在代码隐藏中从 MyControl 派生 MyControl,而您显然不会这样做。

此外,WPF 不允许您从另一个 UserControl 类派生,例如,如果 BobsControl 是 UserControl,则您不能编写

。 ,如果您派生的顶级元素是自定义控件(未在 XAML 中定义),您可以这样做。 >可以创建您自己的顶级元素作为自定义控件,然后从中派生“用户”控件(如果您确实遵循此路线,您可能希望从 ContentControl 或派生您的自定义控件。应用 ContentPropertyAttribute,以便您的顶级元素可以轻松包含其他 XAML。)

No. The XAML is declaring what a MyControl visually is, just as the code-behind is defining what a MyControl behaviourally is. Defining the visuals of a MyControl in terms of a MyControl wouldn't really make sense: it's the equivalent of, in the code-behind, deriving MyControl from MyControl, which you obviously wouldn't do.

In addition, WPF doesn't let you derive one UserControl class from another e.g. if BobsControl is a UserControl then you can't write <local:BobsControl x:Class="MyNamespace.MyControl... /> either. I believe this is because UserControls have a visual appearance (content) baked into their XAML and the content of the derived class would have to replace the content of the base class, so the visual inheritance is generally not useful.

However, you can do it if the top-level element you're deriving from is a custom control. Custom controls are lookless (not defined in XAML). So you can create your own top-level element as a custom control, and then derive "user" controls from that. (If you do go down this route, you'll probably want to derive your custom control from ContentControl or apply ContentPropertyAttribute, so that your top-level element can easily contain other XAML.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文