WPF:不能设置属性元素的怪事性能 [英] WPF: Cannot set properties on property elements weirdness

查看:337
本文介绍了WPF:不能设置属性元素的怪事性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人的TextBlock _caption =新的TextBlock();

公共TextBlock的字幕
{
    {返回_caption; }
    集合{_caption =价值; }
}

< L:CustomPanel>
    < L:CustomPanel.Caption文本=标题文本字号=18前景=白/>
< / L:CustomPanel>
 

让我有以下错误:
不能在属性元素设置的属性。

如果我使用:

 < L:CustomPanel>
    < L:CustomPanel.Caption>
        < TextBlock的文本=标题文本字号=18前景=白/>
    < / L:CustomPanel.Caption>
< / L:CustomPanel>
 

我的TextBlock显示了罚款,但它是嵌套在像这样的另一个TextBlock的,它甚至似乎将自身添加的Caption属性之外:

 < L:CustomPanel>
    < L:CustomPanel.Caption>
        <的TextBlock>
             < InlineUIContainer>
                 < TextBlock的文本=标题文本字号=18前景=白/>
             < / InlineUIContainer>
        < / TextBlock的>
    < / L:CustomPanel.Caption>

    <的TextBlock>
         < InlineUIContainer>
             < TextBlock的文本=标题文本字号=18前景=白/>
         < / InlineUIContainer>
    < / TextBlock的>
< / L:CustomPanel>
 

正如你可能已经猜到了,我想我的code做的是一个自定义面板上设置的XAML我的Caption属性,如果这是可能的。

我也试过同样的code用的DependencyProperty无济于事。

所以,任何人都可以帮助我解决这个问题?

解决方案

我可以解释一下什么错误,以及如何解决它。

首先,

 < L:CustomPanel>
  < L:CustomPanel.Caption文本=标题文本字号=18前景=白/>
 

是一个简单的语法错误。该< L:CustomPanel.Caption> 的语法不接受XML属性 - 属性值必须是该元素在

这是正确的属性元素语法:

 < L:CustomPanel>
  < L:CustomPanel.Caption>
    < TextBlock的文本=标题文本字号=18前景=白/>
  < / L:CustomPanel.Caption>
< / L:CustomPanel>
 

不过:<​​/ P>

  1. 属性元素语法只适用于DependencyProperties(所以也没你的CLR属性的工作)和
  2. 属性元素语法永远荣誉的属性类型的ContentPropertyAttribute

由于TextBlock中有一个[ContentPropertyAttribute(内联),该属性元素语法正试图将TextBlock添加到内联集合。

解决方法很简单:声明你的财产作为DependencyProperty的类型的UIElement ,而不是类型的的TextBlock 。这具有不限制的内容显示到刚才TextBlock的额外的优点。如果你确实想将其限制只是一个TextBlock,你可以使用一个验证回调。

 公共UIElement的内容{获得{...
公共静态只读的DependencyProperty ContentProperty = ...
 

private TextBlock _caption = new TextBlock();

public TextBlock Caption  
{  
    get { return _caption; }  
    set { _caption = value; }  
}

<l:CustomPanel>  
    <l:CustomPanel.Caption Text="Caption text" FontSize="18" Foreground="White" />  
</l:CustomPanel>

Gives me the following error:
Cannot set properties on property elements.

If I use:

<l:CustomPanel>  
    <l:CustomPanel.Caption>
        <TextBlock Text="Caption text" FontSize="18" Foreground="White" /> 
    </l:CustomPanel.Caption>
</l:CustomPanel>

My TextBlock shows up fine but it's nested inside another TextBlock like so, it even seems to add itself outside of the Caption property:

<l:CustomPanel>  
    <l:CustomPanel.Caption>
        <TextBlock>
             <InlineUIContainer>
                 <TextBlock Text="Caption text" FontSize="18" Foreground="White" /> 
             </InlineUIContainer>
        </TextBlock>
    </l:CustomPanel.Caption>

    <TextBlock>
         <InlineUIContainer>
             <TextBlock Text="Caption text" FontSize="18" Foreground="White" /> 
         </InlineUIContainer>
    </TextBlock>
</l:CustomPanel>

As you might have already guessed, what i'd like my code to do is to set my Caption property from XAML on a custom panel, if this is possible.

I've also tried the same code with a DependencyProperty to no avail.

So, anyone that can help me with this problem?

解决方案

I can explain what is going wrong and how to fix it.

First,

<l:CustomPanel>
  <l:CustomPanel.Caption Text="Caption text" FontSize="18" Foreground="White" />

is a simple syntax error. The <l:CustomPanel.Caption> syntax does not accept XML attributes - the property value must be within the element.

This is proper property element syntax:

<l:CustomPanel>    
  <l:CustomPanel.Caption>  
    <TextBlock Text="Caption text" FontSize="18" Foreground="White" />   
  </l:CustomPanel.Caption>  
</l:CustomPanel>

but:

  1. Property element syntax works only with DependencyProperties (so it didn't work with your CLR property) and
  2. Property element syntax always honors the ContentPropertyAttribute of the property type

Since TextBlock has a [ContentPropertyAttribute("Inlines")], the property element syntax is trying to add the TextBlock to the Inlines collection.

The solution is simple: Declare your property as a DependencyProperty of type UIElement instead of type TextBlock. This has the additional advantage of not restricting the display of content to just a TextBlock. If you really do want to restrict it to just a TextBlock, you can use a validation callback.

public UIElement Content { get { ...
public static readonly DependencyProperty ContentProperty = ...

这篇关于WPF:不能设置属性元素的怪事性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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