如何使用命名内容创建 WPF 用户控件

How to create a WPF UserControl with NAMED content(如何使用命名内容创建 WPF 用户控件)

本文介绍了如何使用命名内容创建 WPF 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组带有附加命令和逻辑的控件,它们以相同的方式不断重复使用.我决定创建一个包含所有常用控件和逻辑的用户控件.

I have a set of controls with attached commands and logic that are constantly reused in the same way. I decided to create a user control that holds all the common controls and logic.

但是,我还需要控件来保存可以命名的内容.我尝试了以下方法:

However I also need the control to be able to hold content that can be named. I tried the following:

<UserControl.ContentTemplate>
    <DataTemplate>
        <Button>a reused button</Button>
        <ContentPresenter Content="{TemplateBinding Content}"/>
        <Button>a reused button</Button>
    </DataTemplate>
</UserControl.ContentTemplate>

但是,放置在用户控件内的任何内容似乎都无法命名.例如,如果我按以下方式使用控件:

However it seems any content placed inside the user control cannot be named. For example if I use the control in the following way:

<lib:UserControl1>
     <Button Name="buttonName">content</Button>
</lib:UserControl1>

我收到以下错误:

无法设置名称属性值buttonName"在元素按钮"上.按钮"是元素范围内'UserControl1',它已经有一个定义时注册的名称另一个作用域.

Cannot set Name attribute value 'buttonName' on element 'Button'. 'Button' is under the scope of element 'UserControl1', which already had a name registered when it was defined in another scope.

如果我删除 buttonName,那么它会编译,但是我需要能够命名内容.我怎样才能做到这一点?

If I remove the buttonName, then it compiles, however I need to be able to name the content. How can I achieve this?

推荐答案

在使用 XAML 时,这似乎是不可能的.当我实际上拥有我需要的所有控件时,自定义控件似乎有点过头了,但只需将它们与少量逻辑组合在一起并允许命名内容.

It seems this is not possible when XAML is used. Custom controls seem to be a overkill when I actually have all the controls I need, but just need to group them together with a small bit of logic and allow named content.

京东博客上的解决方案 正如 mackenir 建议的那样,似乎有最好的折衷方案.扩展 JD 的解决方案以允许仍然在 XAML 中定义控件的方法如下:

The solution on JD's blog as mackenir suggests, seems to have the best compromise. A way to extend JD's solution to allow controls to still be defined in XAML could be as follows:

    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);

        var grid = new Grid();
        var content = new ContentPresenter
                          {
                              Content = Content
                          };

        var userControl = new UserControlDefinedInXAML();
        userControl.aStackPanel.Children.Add(content);

        grid.Children.Add(userControl);
        Content = grid;           
    }

在上面的示例中,我创建了一个名为 UserControlDefinedInXAML 的用户控件,它的定义与使用 XAML 的任何普通用户控件一样.在我的 UserControlDefinedInXAML 中,我有一个名为 aStackPanel 的 StackPanel,我希望我的命名内容出现在其中.

In my example above I have created a user control called UserControlDefinedInXAML which is define like any normal user controls using XAML. In my UserControlDefinedInXAML I have a StackPanel called aStackPanel within which I want my named content to appear.

这篇关于如何使用命名内容创建 WPF 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用命名内容创建 WPF 用户控件

基础教程推荐