【发布时间】:2010-12-05 22:38:06
【问题描述】:
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>
给我以下错误:
无法在属性元素上设置属性。
如果我使用:
<l:CustomPanel>
<l:CustomPanel.Caption>
<TextBlock Text="Caption text" FontSize="18" Foreground="White" />
</l:CustomPanel.Caption>
</l:CustomPanel>
我的 TextBlock 显示得很好,但它像这样嵌套在另一个 TextBlock 中,它甚至似乎将自身添加到 Caption 属性之外:
<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>
您可能已经猜到了,如果可能的话,我希望我的代码在自定义面板上设置来自 XAML 的 Caption 属性。
我也用 DependencyProperty 尝试了相同的代码,但无济于事。
那么,谁能帮我解决这个问题?
【问题讨论】:
标签: c# .net wpf xaml properties