我怎么能窝自定义XAML元素? [英] How Can I Nest Custom XAML Elements?

查看:112
本文介绍了我怎么能窝自定义XAML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1">
    <Grid>
        <local:ElementType x:Name="FirstElementName">
            <local:ElementType x:Name="SecondElementName" Grid.Column="1" Grid.Row="1" />
        </local:ElementType>
    </Grid>
</Window>

这是在其他文件...

And this is in other files ...

<Grid x:Name="InternalElementName" x:Class="WpfApplication1.ElementType"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1">
</Grid>

public partial class ElementType : System.Windows.Controls.Grid { }

一切工作正常,但第二个元素。
我得到的错误:
标签上的元素元素类型'不能设置名称属性值SecondElementName。 '的ElementType'是元素的ElementType',它已经有,当它在另一个范围被定义的名称注册的范围下。

Everything works fine, except the second element.
I get the error:
Cannot set Name attribute value 'SecondElementName' on element 'ElementType'. 'ElementType' is under the scope of element 'ElementType', which already had a name registered when it was defined in another scope.

自定义网格正确定义。在code编译和运行,如果我拿出的财产---

The custom grids are defined properly. The code will compile and run if I take out the property ---

x:Name="SecondElementName"

---在Window1.xaml

--- in Window1.xaml

什么是导致此错误?我如何围绕它得到什么呢?我需要窝内的另一个这些自定义网格之一,我需要他们两个人的名字,所以我可以将它们绑定到单独的数据。

What is causing this error? How do I get around it? I need to nest one of these custom grids inside the other one, and I need names on both of them so I can bind them to seperate data.

在此先感谢。

推荐答案

为了知道如何处理嵌套的标记物,对XAML分析器会,除其他事项外,看是否有.NET类定义了一个默认的内容物业作为一个容器,这样的孩子使用。这是与ContentPropertyAttribute

In order to know what to do with nested markup objects, the XAML parser will, among other things, look at whether the .NET class defines a default "content" property to use as a container for such children. This is done with the "ContentPropertyAttribute".

在你的情况,因为我想你想嵌套对象去网格里面,因为一格的孩子们在儿童属性集合,你只需要做到以下几点:

In your case, since I guess you want nested objects to go inside the Grid, and since the children of a grid go in the "Children" property collection, you just need to do the following:

[ContentProperty("Children")]
public partial class ElementType : Grid
{
    // your code here...
}

如果你加入孩子们的控制时,需要做一些逻辑(例如,只允许某些类型是你的元素类型控件的子级),则可以改为从IAddChild继承,并实施的AddChild和AddText方法。

If you need to do some logic when adding children to your control (e.g. only allow certain types to be children of your ElementType control), you can instead inherit from IAddChild, and implement the AddChild and AddText methods.

对于命名的问题,似乎只有无外观控件可以命名的实例化范儿。所以基本上,你可以在里面ElementType.xaml命名的孩子,但没有命名,你实例化元素类型的其他标记的孩子。我想这是对的方式,因为他们优化逻辑树什么的。 一个无外观的控制,而另一方面,仅与code进行控制。如果你把你的班级分成网格的一个普通的老空子所以,它的工作原理:

As for the naming problem, it seems that only lookless controls can have named children in an instanced scope. So basically, you can have named children inside ElementType.xaml, but not named children in other markup where you instantiate ElementType. I guess it's because of the way they optimize the logical tree or something. A lookless control, on the other hand, is a control with only code. So if you turn your class into a plain old empty subclass of Grid, it works:

public class ElementType : Grid
{
}

耶!少code!

Yay! Less code!

这篇关于我怎么能窝自定义XAML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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