如何创建接受内部 XAML 控件的 UserControl? [英] How to I create a UserControl that accepts inner XAML controls?
本文介绍了如何创建接受内部 XAML 控件的 UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于 Stackpanel,我可以将文本框和按钮等控件作为内部 XAML:
Similar to the Stackpanel, where I can have controls such as textboxes and buttons as inner-XAML:
<StackPanel>
<Button Content="OK">
<Button Content="Cancel">
<Button Content="Apply">
</StackPanel>
我需要的是:
What I need is:
<myControls:myOptionsList>
<Button Content="OK">
<Button Content="Cancel">
<Button Content="Apply">
</myControls:myOptionsList>
推荐答案
假设你的 UserControl 有一个内部 Panel 元素,其他元素应该放在那里,比如这样:
Assuming your UserControl has an inner Panel element where the additional elements should be placed, i.e. like this:
<UserControl x:Class="YourNamespace.OptionsControl" ...>
<Grid>
<GroupBox Header="Options">
<StackPanel x:Name="optionsPanel"/>
</GroupBox>
</Grid>
</UserControl>
您可以简单地将该面板的 Children
属性公开为标记为 UserControl 的 ContentProperty
的 UIElementCollection 属性:
you may simply expose that Panel's Children
property as a UIElementCollection property that is marked to be the ContentProperty
of the UserControl:
[ContentProperty(nameof(Options))]
public partial class OptionsControl : UserControl
{
public UIElementCollection Options
{
get { return optionsPanel.Children; }
}
...
}
这篇关于如何创建接受内部 XAML 控件的 UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文