带有子标签的WPF用户控件

时间:2014-01-27 05:24:28

标签: c# wpf visual-studio-2012 user-controls

我想知道是否可以制作WPF UserControl并且是否有子/子UserControls? 如果可能的话,最好的方法是什么,或者我应该从哪里开始。

例如:

<CustomMenu>
     <CustomMenuItem x:Name="menuItem1" />
     <CustomMenuItem x:Name="menuItem2" />
     ....
</CustomMenu>

我的想法是让UserControl获取一些信息和属性,并且其子级具有识别它们的其他属性。

我试图在父UserControl中创建一个变量,例如UIElementCollection,并在XAML file中表示为StackPanel,但这并不是我想要的特别允许的有其他类型的孩子,我不能为父UserControl设置任何属性,如宽度

我仍然是WPF的初学者,所以我知道我必须错过并且不知道很多东西所以请耐心等待。

我感谢所有的帮助或指导。 谢谢。

修改

应用完美运行的@Dennis(https://stackoverflow.com/a/21373956/3157993)解决方案后,我注意到一个奇怪的行为,如果我在另一个窗口或页面中有另一个UserControl实例,它将累积项目第一个菜单,在第二个菜单中。例如:如果我在家中有一个菜单,而在另一个页面中有另一个菜单,当我打开Home时,它会显示完美,但是如果我打开另一个页面,我将在第一页的旁边有菜单项通过第二页中的菜单项。 - Nirvana Priest

2 个答案:

答案 0 :(得分:2)

  

可以创建一个WPF UserControl并有子/子   UserControls与否?

是的,有可能。也许,简短的样本会有所帮助 父控制(XAML):

<UserControl x:Class="WpfApplication4.CustomMenu"
             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>
        <ItemsControl ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
    </Grid>
</UserControl>

父控制(代码隐藏):

public partial class CustomMenu : UserControl
{
    public CustomMenu()
    {
        InitializeComponent();
    }

    public ObservableCollection<CustomMenuItem> Items
    {
        get { return (ObservableCollection<CustomMenuItem>)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
        "Items", 
        typeof(ObservableCollection<CustomMenuItem>), 
        typeof(CustomMenu), 
        new PropertyMetadata(new ObservableCollection<CustomMenuItem>()));
}

儿童控制(XAML):

<UserControl x:Class="WpfApplication4.CustomMenuItem"
             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>
        <TextBlock Text="Hello, world!"/>
    </Grid>
</UserControl>

用法:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:CustomMenu>
            <local:CustomMenu.Items>
                <local:CustomMenuItem />
                <local:CustomMenuItem />
                <local:CustomMenuItem />
            </local:CustomMenu.Items>
        </local:CustomMenu>
    </Grid>
</Window>

但目前尚不清楚,你想要实现什么目标。也许,您需要custom control,继承自HeaderedItemsControl

答案 1 :(得分:1)

您可以通过扩展System.Windows.Controls.Panel类创建控件:

MSDN: Panel Class

示例代码可以在以下网址下载:

Create a Custom Content-Wrapping Panel Sample