将窗口标题替换为MahApps.Metro无边框窗口的菜单

时间:2014-01-12 07:06:41

标签: c# wpf xaml mahapps.metro

我正在使用MahApps.Metro控件开发一个没有边框的WPF窗口应用程序。

我希望我的菜单通常是窗口标题的位置(标题栏的左侧)。如下图所示:

enter image description here

到目前为止我看到的图像如下:

enter image description here

我已尝试设置HorizontalAlignment="Left",但菜单组仍保留在标题栏的右侧。

此代码:

<Controls:MetroWindow.WindowCommands>        
    <Controls:WindowCommands HorizontalAlignment="Left">
        <Menu IsMainMenu="True" x:Name="mnuMainMenu" Height="28" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="12" Background="Transparent" Width="Auto" >

            <MenuItem Header="_File" x:Name="mnuFile" Visibility="Visible" Background="Transparent">
                <MenuItem Header="_Open" x:Name="mnuOpen" Background="Transparent" Command="{Binding MenuOpenCommand}" />

                <MenuItem Header="_Exit" x:Name="mnuExit" Click="btnExit_Click" Background="Transparent"/>
            </MenuItem>

            <MenuItem Header="_Tools">
                <MenuItem Header="_Repeat" x:Name="mnuRepete" Background="Transparent" >
                    <MenuItem Header="Repeat None" Command="{Binding RepeatNoneCommand}" IsCheckable="True"/>
                    <MenuItem Header="Repeat One" Command="{Binding RepeatOneCommand}" IsCheckable="True"/>
                    <MenuItem Header="Repeat All" Command="{Binding RepeatAllCommand}" IsCheckable="True"/>
                </MenuItem>
            </MenuItem>

            <MenuItem Header="_Store" x:Name="smOnlineMode" Background="Transparent" Click="smOnlineMode_Click" IsCheckable="True" />
            <MenuItem Header="_Play Mode" x:Name="smPlayMode" Background="Transparent" Click="smPlayMode_Click" IsCheckable="True" IsChecked="True"/>


            <MenuItem Header="_Play">
                <MenuItem Header="_Play" x:Name="mnuPlay" Background="Transparent"  Command="{Binding PlayCommand}"/>
                <MenuItem Header="P_ause" x:Name="mnuPause" Background="Transparent" Command="{Binding PauseCommand}"/>
                <MenuItem Header="_Stop" x:Name="mnuStop" Background="Transparent" Command="{Binding StopCommand}"/>
                <Separator/>
                <MenuItem Header="_Next" x:Name="mnuNext" Background="Transparent" Command="{Binding NextTrackCommand}"/>
                <MenuItem Header="P_revious" x:Name="mnuPrevious" Background="Transparent" Command="{Binding PreviousTrackCommand}" />
                <MenuItem Header="_Mute/UnMute" x:Name="smnuMute" Background="Transparent" Command="{Binding MuteSoundCommand}" />
                <!--Command="{Binding MuteSoundCommand}"-->

            </MenuItem>

            <MenuItem Header="_Help">

                <MenuItem Header="_Help" x:Name="smnuOnlineHelp" Background="Transparent" Click="smnuHelp_Click" />
                <Separator />
                <MenuItem Header="_Register Player" x:Name="smnuRegister" Background="Transparent" Click="smnuRegisterPlayer" />

                <MenuItem Header="_About Codero Music Player" x:Name="smnuAbout" Background="Transparent" Click="smnuAboutClick" />
            </MenuItem>
        </Menu>
    </Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>

4 个答案:

答案 0 :(得分:6)

你可以做这样的事情

  1. 从标题栏中删除标题
  2. 添加MetroWindow.LeftWindowCommands代码
  3. 在LeftWindowCommands
  4. 中添加windows命令标记
  5. 放置一个堆叠面板或网格,并在标题栏中放置您想要的内容
  6. 代码:

           <controls:MetroWindow.LeftWindowCommands>
              <controls:WindowCommands>
                <StackPanel Name="menuHolder" Orientation="Horizontal">
                    <TextBlock Padding="10,5,10,5" Text="My Window"></TextBlock>
                    <Menu Name="mymenu" Margin="0,5,0,0">
                        <MenuItem Name="File" Header="File">
                            <MenuItem Name="Open" Header="Open"/>
                            <MenuItem Name="Close" Header="Close"/>
                        </MenuItem>
                        <MenuItem Name="Edit" Header="Edit">
                            <MenuItem Name="Copy" Header="Copy"/>
                            <MenuItem Name="Paste" Header="Paste"/>
                        </MenuItem>
                    </Menu>
                </StackPanel>
            </controls:WindowCommands>     
    

答案 1 :(得分:0)

创建StackPanel,将您的菜单放入StackPanel并设置属性HorizontalAlignment=Left或尝试再次使用保证金属性

答案 2 :(得分:0)

你必须为自己的东西重新安排MetroWindow。满足您需求的最简单方法是创建自定义资源字典并将MetroWindow.xaml复制到其中,并将以下行更改为Grid.Column="0"MetroWindow.xaml

但不要忘记在App.xaml中加载修改后的资源。

答案 3 :(得分:0)

在MahApps.Metro 1.6.5中对我有用的解决方案是将TitleTemplate依赖项属性与主窗口中的MenuBar和Title Textblock绑定,如下所示:

MainWindow.xaml:

<Controls:MetroWindow
        x:Class="MahAppsMetroDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MahAppsMetroDemo"
        mc:Ignorable="d"
        Title="ILSpy"
        Height="450" Width="800"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Icon="/MahAppsMetroDemo;component/Resources/ILSpy.ico"
    >
    <Controls:MetroWindow.TitleTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Menu
                    Grid.Column="0"
                    Margin="6,0">
                <MenuItem Name="File" Header="File">
                    <MenuItem Name="Open" Header="Open"/>
                    <MenuItem Name="Close" Header="Close"/>
                </MenuItem>
                <MenuItem Name="Edit" Header="Edit">
                    <MenuItem Name="Copy" Header="Copy"/>
                    <MenuItem Name="Paste" Header="Paste"/>
                </MenuItem>
            </Menu>
                <TextBlock
                    Grid.Column="1"
                    Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}},Path=Title}"
                    HorizontalAlignment="Left" VerticalAlignment="Center"
                    Padding="10,5,10,5"
                    Margin="6,0"
                    FontSize="16"
                    FontWeight="Bold"
                    />
            </Grid>
        </DataTemplate>
    </Controls:MetroWindow.TitleTemplate>
    <Grid>

    </Grid>
</Controls:MetroWindow>

MainWindow.cs

namespace MahAppsMetroDemo
{
    using MahApps.Metro.Controls;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}