将控件的 XAML 内容设置为该控件的属性
我有一个这样的用户控件:
public partial class MyControl : UserControl
{
private static readonly DependencyProperty pageContentProperty = DependencyProperty.Register("PageContent", typeof(UIElement), typeof(ActionPage), new PropertyMetadata(null));
public UIElement PageContent
{
get { return (UIElement)base.GetValue(pageContentProperty); }
set { base.SetValue(pageContentProperty, value); }
}
public MyControl()
{
InitializeComponent();
}
// More code
}
现在,如果我在 XAML 中使用它,我必须这样做:
<l:MyControl>
<l:MyControl.PageContent>
<TextBlock Text="Lorum Ipsum"/>
</l:MyControl.PageContent>
</l:MyControl>
但我只想这样做:
<l:MyControl>
<TextBlock Text="Lorum Ipsum"/>
</l:MyControl>
目前,如果我这样做,它将用 TextBlock
替换控件的整个内容(这对于普通的UserControl
来说是有意义的)但是我怎样才能覆盖这种行为呢?
I have a user control as such:
public partial class MyControl : UserControl
{
private static readonly DependencyProperty pageContentProperty = DependencyProperty.Register("PageContent", typeof(UIElement), typeof(ActionPage), new PropertyMetadata(null));
public UIElement PageContent
{
get { return (UIElement)base.GetValue(pageContentProperty); }
set { base.SetValue(pageContentProperty, value); }
}
public MyControl()
{
InitializeComponent();
}
// More code
}
Now if I use it in XAML I have to do:
<l:MyControl>
<l:MyControl.PageContent>
<TextBlock Text="Lorum Ipsum"/>
</l:MyControl.PageContent>
</l:MyControl>
But I want to just do:
<l:MyControl>
<TextBlock Text="Lorum Ipsum"/>
</l:MyControl>
Currently if I do this it replace the entire content of the control with the TextBlock
(which makes sense for a normal UserControl
) but how can I override this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将
ContentProperty
属性添加到PageContent
属性中:-You might try adding the
ContentProperty
attribute to yourPageContent
property:-