C# 在自定义用户控件中选择子控件

C# 在自定义用户控件中选择子控件,c#,wpf,user-controls,custom-controls,design-time,C#,Wpf,User Controls,Custom Controls,Design Time,我正在构建一个可以处理第三方模块的应用程序 所有模块必须具有相同的外观,因此为了方便起见,我创建了一个.dll 问题使用自定义usercontrol模板ModuleChrome时,开发人员无法选择名为ToolBar&Body的子容器及其各自的子容器。这确实很不方便,因为在designer中选择子元素时,会选择整个usercontrol(ModuleChrome) 问题如何在designer中选择工具栏和主体内的控件 ModuleChrome课程 public class ModuleChrome

我正在构建一个可以处理第三方模块的应用程序

所有模块必须具有相同的外观,因此为了方便起见,我创建了一个.dll

问题使用自定义usercontrol模板ModuleChrome时,开发人员无法选择名为ToolBar&Body的子容器及其各自的子容器。这确实很不方便,因为在designer中选择子元素时,会选择整个usercontrol(ModuleChrome)

问题如何在designer中选择工具栏和主体内的控件

ModuleChrome课程

public class ModuleChrome : UserControl
{

    public static readonly DependencyProperty ToolBarProperty = DependencyProperty.Register("ToolBar", typeof(object), typeof(ModuleChrome), new UIPropertyMetadata(null));

    public static readonly DependencyProperty BodyProperty = DependencyProperty.Register("Body", typeof(object), typeof(ModuleChrome), new UIPropertyMetadata(null));

    public object ToolBar
    {
        get { return (object)GetValue(ToolBarProperty); }
        set { SetValue(ToolBarProperty, value); }
    }

    public object Body
    {
        get { return (object)GetValue(BodyProperty); }
        set { SetValue(BodyProperty, value); }
    }

    static ModuleChrome()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ModuleChrome), new FrameworkPropertyMetadata(typeof(ModuleChrome)));
    }
}
以及相应的风格

<Style TargetType="{x:Type local:ModuleChrome}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ModuleChrome}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <!--Main Content-->
                    <ContentControl Grid.Row="1">
                        <ContentControl Content="{TemplateBinding Body}"/>
                    </ContentControl>

                    <!--Toolbar-->
                    <Grid Grid.Row="0" Background="White">
                        <Grid.Effect>
                            <DropShadowEffect ShadowDepth="0" BlurRadius="4" Opacity="0.5"/>
                        </Grid.Effect>
                        <ContentControl Content="{TemplateBinding ToolBar}"/>
                    </Grid>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
和xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="1" x:Name="gridBody"/>

    <Grid x:Name="gridToolBar" Grid.Row="0" Background="White">
        <Grid.Effect>
            <DropShadowEffect ShadowDepth="0" BlurRadius="4" Opacity="0.5"/>
        </Grid.Effect>
    </Grid>
</Grid>

此方法存在问题

这是在单个usercontrol中,我可以在每个模块(usercontrol)中使用它,但我需要将此功能转移到我的CustomControl中,因为usercontrol不允许在将其用作模板时命名内容

如果我们找到了解决方案,我会更新答案

public partial class MultiChildDemo : UserControl
{
    public static readonly DependencyPropertyKey ToolBarProperty = DependencyProperty.RegisterReadOnly(
        "ToolBar",
        typeof(UIElementCollection),
        typeof(MultiChildDemo),
        new PropertyMetadata());

    public static readonly DependencyPropertyKey BodyProperty = DependencyProperty.RegisterReadOnly(
        "Body",
        typeof(UIElementCollection),
        typeof(MultiChildDemo),
        new PropertyMetadata());


    public UIElementCollection ToolBar
    {
        get { return (UIElementCollection)GetValue(ToolBarProperty.DependencyProperty); }
        set { SetValue(ToolBarProperty, value); }
    }

    public UIElementCollection Body
    {
        get { return (UIElementCollection)GetValue(BodyProperty.DependencyProperty); }
        set { SetValue(BodyProperty, value); }
    }

    public MultiChildDemo()
    {
        InitializeComponent();
        ToolBar = gridToolBar.Children;
        Body = gridBody.Children;
    }
}
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="1" x:Name="gridBody"/>

    <Grid x:Name="gridToolBar" Grid.Row="0" Background="White">
        <Grid.Effect>
            <DropShadowEffect ShadowDepth="0" BlurRadius="4" Opacity="0.5"/>
        </Grid.Effect>
    </Grid>
</Grid>