【问题标题】:Adding items to a menu in a usercontrol将项目添加到用户控件中的菜单
【发布时间】:2013-02-14 09:07:07
【问题描述】:

我创建了一个包含两个项目的用户控件:一个按钮和一个菜单。下面的代码是一个极其简化的版本,仍然存在同样的问题(我将在下面提到)。

<UserControl x:Class="gemsTouchLensApplication.Controls.ButtonAndMenu"
         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">
    <StackPanel Orientation="Horizontal">
        <Button/>
        <Menu/>
    </StackPanel>
</UserControl>

我希望能够用这个用户控件做两件事:将项目添加到菜单和设置按钮的内容。

我希望能够以常规方式将项目添加到菜单中

<Menu>
    <MenuItem Header="1"/>
    <MenuItem Header="2"/>
    <MenuItem Header="3"/>
        <MenuItem Header="4"/>
        <MenuItem Header="5"/>
    etc...
</Menu>

但是,当我在将用户控件添加到堆栈面板后尝试执行此操作时

<Window>
    <StackPanel>
        <cntrls:ButtonAndMenu>
            <MenuItem Header="1"/>
            <MenuItem Header="2"/> <!--Comment this out and the error disappears-->
        </cntrls:ButtonAndMenu>
    </StackPanel>
</Window>

发生了意想不到的事情。我只能向用户控件添加一个项目。如果我添加多个项目,我会收到错误:

属性“内容”只能设置一次。

当只添加一个项目时,控件的视觉效果更接近于按钮,而不是菜单。

我怀疑通过向用户控件添加控件,WPF 引擎默认设置按钮的内容,而不是向菜单添加控件。

如何修改用户控件以便消费者可以:

  1. 使用常规符号向菜单添加项目
  2. 还是设置按钮的内容

【问题讨论】:

    标签: c# wpf user-controls


    【解决方案1】:

    知道怎么做。

    我创建了一个名为 MenuItems 的依赖属性,并将菜单的项目源绑定到该属性。我还必须在我的用户控件的类上设置一个属性。

    [ContentProperty("MenuItems")]
    public partial class ButtonAndMenu: UserControl
    {
        public ObservableCollection<DependencyObject> MenuItems
        {
            get { return (ObservableCollection<DependencyObject>)GetValue(MenuItemsProperty); }
            set { SetValue(MenuItemsProperty, value); }
        }
    
        internal static readonly DependencyProperty MenuItemsProperty = DependencyProperty.Register(
            "MenuItems", typeof(ObservableCollection<DependencyObject>), typeof(ButtonAndMenu),
            new FrameworkPropertyMetadata(new ObservableCollection<DependencyObject>()));
    }
    

    XAML 变成:

    <UserControl x:Class="gemsTouchLensApplication.Controls.ButtonAndMenu"
             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"
             x:Name="root">
        <StackPanel Orientation="Horizontal">
            <Button/>
            <Menu ItemsSource={Binding ElementName=root, Path=MenuItems}/>
        </StackPanel>
    </UserControl>
    

    然后,在主窗口中,我可以执行以下操作:

    <Window>
        <StackPanel>
            <cntrls:ButtonAndMenu>
                <MenuItem Header="1"/>
                <MenuItem Header="2"/>
                <MenuItem Header="3"/>
                <!--etc...    You can keep adding items here-->
            </cntrls:ButtonAndMenu>
        </StackPanel>
    </Window>
    

    所以,总结一下:

    1. 创建一个作为集合的依赖属性
    2. 将类的 ContentProperty 属性设置为该依赖属性
    3. 将菜单的 ItemsSource 绑定到该属性

    【讨论】:

      【解决方案2】:

      这是因为MenuItem 派生自ItemsControlUserControlContentControl。您必须将ButtonAndMenu 设为ItemsControl。你为什么不做一个自定义的 MenuItemControl?

      编辑:

      此链接可能会有所帮助。

      1)Creating and consuming a custom WPF control。该演示展示了如何将ListBox 自定义为ColorPickerControl1

      您可以对 MenuItemControl 使用相同的概念。

      您将不再使用 UserControl,您的 CustomControl 将看起来像。为此,您需要研究如何在 WPF 中制作自定义控件。

      public class MyMenuItem : MenuItem
      { 
          // add your custom work here
      }
      

      【讨论】:

      • 我不完全理解你的建议。自定义 MenuItemControl 的目的是什么?您建议我如何使用它?
      • 检查已编辑的部分。你必须研究它并创造它。
      • 我浏览了您的链接,但我仍然不确定如何创建自定义 MenuItem 将允许我将自定义 MenuItem 的多个实例添加到我的用户控件中的菜单。
      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多