如何使WPF UserControl充当容器

如何使WPF UserControl充当容器,wpf,user-controls,Wpf,User Controls,我正在尝试创建一个工具栏控件,该控件可以使用边框和标签对所选按钮进行分组。如果已经有一个内置控件可以这样做,那么我可以使用它来代替构建UserControl 如果没有,那么我想要构建的是一个UserControl,它允许我输入一到多个ImageButton UserControl,并设置如下所示的GroupLabel文本。这可以在WPF中完成吗 <User_Controls:ToolbarGroup GroupLabel="Entity">

我正在尝试创建一个工具栏控件,该控件可以使用边框和标签对所选按钮进行分组。如果已经有一个内置控件可以这样做,那么我可以使用它来代替构建UserControl

如果没有,那么我想要构建的是一个UserControl,它允许我输入一到多个ImageButton UserControl,并设置如下所示的GroupLabel文本。这可以在WPF中完成吗

        <User_Controls:ToolbarGroup GroupLabel="Entity">
                <User_Controls:ImageButton ButtonText="Entity Setup"/>
                <User_Controls:ImageButton ButtonText="New Entity"/>
        </User_Controls:ToolbarGroup>


PS:我会发布一张图片,但这个古怪的论坛不允许我发布图片。

如果我没有弄错的话,我想你也可以通过这种方式来实现,而且在鼠标“插入并离开”活动中,你可以点击按钮。 要设置文本,可以使用网格和其中的标签来设置文本,并在其下方设置图像按钮

<UserControl x:Class="ABC.View.Oats"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Image Source="{Binding Image}" Stretch="Fill"/>
    </Grid>
</UserControl>

我想您要找的是一个GroupBox,它有一个header属性,您可以在其中设置标签

大概是这样的:

<GroupBox Width="300" Height="100">
        <GroupBox.Header>
            <Label>Text</Label>
        </GroupBox.Header>
        <StackPanel>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
        </StackPanel>
    </GroupBox>
public class customGroupBox: GroupBox{
 ....Add whatever you need here
}

正文

我还建议使用groupbox,它似乎正是您想要它做的,而且看起来很整洁。下面是一些关于如何使用它们的示例:

另一方面,如果您认为groupbox是不够的,那么可以创建一个继承自groupbox的控件,并对其进行扩展和添加所需的内容。它看起来是这样的:

<GroupBox Width="300" Height="100">
        <GroupBox.Header>
            <Label>Text</Label>
        </GroupBox.Header>
        <StackPanel>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
        </StackPanel>
    </GroupBox>
public class customGroupBox: GroupBox{
 ....Add whatever you need here
}

谢谢你的回复。我尝试了GroupBox,但它不是我们想要的布局,因为我们希望标签位于按钮下方并居中。我从来都找不到向UserControl添加集合的方法。也许我把它称为容器时问得不对。下面的代码可以工作,但并不优雅。我想要的东西将包装在一个用户控件布局,并允许我添加一个可变数量的按钮到每个工具栏组


实现这一点的一种方法是使用自定义样式的ItemsControl

然后可以重用它,并将其绑定到不同的数据

请原谅,这是手写的

在你的资源中

<Style x:Key="ToolbarGroupItemsControlStyle" TargetType="ItemsControl">
    ...
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ItemsControl">
                <Grid>
                    ... XAML to form your group with a binding to the
                    ... group name
                    <ItemsPresenter/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate x:Key="ToolbarGroupItemTemplate">
    <Grid>
        ... XAML and binding for each toolbar group item ...
    </Grid>
</DataTemplate>

...
... XAML来组成组,并绑定到
... 组名
... 每个工具栏组项的XAML和绑定。。。
在您的XAML中

<ItemsControl 
    Style="{DynamicResource ToolbarGroupItemsControlStyle}"
    ItemsSource="{Binding ToolbarGroupItems}"
    ItemTemplate="{DynamicResource ToolbarGroupItemTemplate"/>

如果上面的资源位于应用程序级别,则可以将上面的ItemsControl放置在所需的任何窗口/用户控件上

您的ItemsSource需要是您创建的自定义类型的集合,该集合具有按钮文本等的绑定


我希望这会有所帮助。

您可以改为添加图像链接。