C# WPF Mahapps Hamburger菜单使用扩展构造函数打开用户控件

C# WPF Mahapps Hamburger菜单使用扩展构造函数打开用户控件,c#,wpf,menu,C#,Wpf,Menu,我用的就是这个汉堡菜单的例子 只要视图具有默认构造函数,这就可以正常工作。 但是,我需要将一个对象从实现汉堡菜单的MetroWindow传递到通过单击菜单项调用的UserControl 菜单项的XAML代码如下所示 <controls:HamburgerMenu.ItemsSource> <controls:HamburgerMenuItemCollection> <controls:HamburgerMenuGlyphItem Glyph

我用的就是这个汉堡菜单的例子

只要视图具有默认构造函数,这就可以正常工作。 但是,我需要将一个对象从实现汉堡菜单的MetroWindow传递到通过单击菜单项调用的UserControl

菜单项的XAML代码如下所示

<controls:HamburgerMenu.ItemsSource>
    <controls:HamburgerMenuItemCollection>
        <controls:HamburgerMenuGlyphItem Glyph="" Label="Home">
            <controls:HamburgerMenuGlyphItem.Tag>
                <views:main />
            </controls:HamburgerMenuGlyphItem.Tag>
        </controls:HamburgerMenuGlyphItem>

        <controls:HamburgerMenuGlyphItem Glyph="" Label="Private">
            <controls:HamburgerMenuGlyphItem.Tag>
                <views:private />
            </controls:HamburgerMenuGlyphItem.Tag>
        </controls:HamburgerMenuGlyphItem>

        <controls:HamburgerMenuGlyphItem Glyph="" Label="Settings">
            <controls:HamburgerMenuGlyphItem.Tag>
                <views:settings />
            </controls:HamburgerMenuGlyphItem.Tag>
        </controls:HamburgerMenuGlyphItem>
    </controls:HamburgerMenuItemCollection>
</controls:HamburgerMenu.ItemsSource>
“主”用户控件的XAML


“TesterConfig”是我项目中的一个对象,在父MetroWindow中声明。如何将引用传递给UserControl

谢谢你的帮助!

谢谢@mm8。 根据你的建议,我是这样做的

XAML(菜单项)

我根据标签创建了一个开关。 谢谢你的帮助

最好的,
Andy

除非视图具有无参数构造函数,否则无法在XAML中创建这样的视图实例:
。这不受支持。以编程方式设置
标记
属性。如何将事件菜单控件项Click绑定到HamburgerMenuGlyphItem?
 <controls:HamburgerMenuGlyphItem.Tag>
                <views:main />
 </controls:HamburgerMenuGlyphItem.Tag>
#region PUBLIC_PROPERTIES
TesterConfig.TesterConfig tc;
#endregion
public main(TesterConfig.TesterConfig configuration)
{
    InitializeComponent();
    tc = configuration;
}
<UserControl x:Class="TestsystemConfiguration.Views.main"
      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" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
      xmlns:local="clr-namespace:TestsystemConfiguration.Views"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800">

    <Grid x:Name="gridMain">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="130"/>
            <ColumnDefinition Width="423*"/>
            <ColumnDefinition Width="175"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="8"/>
            <RowDefinition Height="36"/>
            <RowDefinition Height="36"/>
            <RowDefinition Height="36"/>
            <RowDefinition Height="11*"/>
            <RowDefinition Height="45*"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Image x:Name="imageLogo" Grid.Column="2" Source="pack://siteoforigin:,,,/Resources/logo.png" Margin="5" Grid.RowSpan="1"/>
        <ComboBox x:Name="cbWorkingStation" IsEnabled="False"  Grid.Column="1" Margin="5,2,10,2" Grid.Row="2" SelectionChanged="cbWorkingStation_SelectionChanged"/>
        <Button x:Name="btnOpenDocs" Content="Doku öffnen" Style="{StaticResource AccentedSquareButtonStyle}" Grid.Column="2" Margin="5" Grid.Row="7" Controls:ControlsHelper.ContentCharacterCasing="Upper"/>
        <Label Content="Arbeitsplatz:" Grid.Row="2" FontWeight="Bold"/>

    </Grid>
</UserControl>
<Controls:HamburgerMenuGlyphItem Glyph=""
                        Label="Home"
                        Tag="main">
</Controls:HamburgerMenuGlyphItem>
    private void MenuControl_ItemClick(object sender, ItemClickEventArgs e)
    {
        HamburgerMenuGlyphItem i = e.ClickedItem as HamburgerMenuGlyphItem;
        if(i != null)
        {
            UserControl uc = new UserControl();
            switch(i.Tag.ToString())
            {
                case "main":
                    uc = new Views.main(tc);
                    break;

                case "testsystems":
                    uc = new Views.testsystems();
                    break;
            }
            i.Tag = uc;
            this.MenuControl.Content = i;
        }
    }