【问题标题】:Menu control (WPF) issues菜单控制 (WPF) 问题
【发布时间】:2020-02-17 10:50:45
【问题描述】:

我想在我的应用程序中创建一个可扩展的菜单控件。我使用this answer 创建了一个可伸缩窗口,然后将菜单控件插入其中。我有一些问题,找不到解决方案。

如何设置菜单项的“中心”垂直调整?现在它们停靠在顶部。有什么方法可以将第一项(在我的情况下为“文件”)向右移动一点?

在调整窗口大小时是否可以避免剪切菜单面板?现在,即使我将内部网格的上边框向下移动一点,菜单面板也会被减半。

我的代码及其全屏和小比例的结果如下:

<Window x:Class="DigitalSteelCastingWPF.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:DigitalSteelCastingWPF"
        mc:Ignorable="d"
        Title="Digital Steel Casting"
        Name="myMainWindow"
        Height="1056" Width="1936"
        WindowStartupLocation="CenterScreen" WindowState="Maximized"
        FontSize="24"
        HorizontalAlignment="Center" VerticalAlignment="Center"
        WindowStyle="ThreeDBorderWindow">

    <Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged">

        <Grid.LayoutTransform>
            <ScaleTransform x:Name="ApplicationScaleTransform"
                        CenterX="0"
                        CenterY="0"
                        ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
                        ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
        </Grid.LayoutTransform>

        <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Height="1025" Width="1928">

            <StackPanel VerticalAlignment="Top">
                <Menu Width="Auto" Height="50" VerticalAlignment="Center" FontSize="24">
                    <MenuItem Header="_File">
                        <MenuItem x:Name="New" Header="_New" Height="50" HorizontalAlignment="Left" Width="250"/>
                        <MenuItem x:Name="Open" Header="_Open" Height="50" HorizontalAlignment="Left" Width="250"/>
                        <MenuItem x:Name="Exit" Header="_Exit" Height="50" HorizontalAlignment="Left" Width="250"/>
                    </MenuItem>
                    <MenuItem Header="_Tools">
                        <MenuItem x:Name="Options" Header="O_ptions" Height="50" HorizontalAlignment="Left" Width="250"/>
                    </MenuItem>
                </Menu>
            </StackPanel>

        </Grid>

    </Grid>

</Window>

【问题讨论】:

    标签: c# wpf xaml menu visual-studio-2019


    【解决方案1】:

    你可以添加一个 Viewbox

     <Grid>
            ***<Viewbox Stretch="Fill">***
                <Grid VerticalAlignment="Center" HorizontalAlignment="Center"  Height="1025" Width="1928">
    
                    <StackPanel VerticalAlignment="Top">
                        <Menu Width="Auto" Height="50" VerticalAlignment="Center" FontSize="24">
                            <MenuItem Header="_File">
                                <MenuItem x:Name="New" Header="_New" Height="50" HorizontalAlignment="Left" Width="250"/>
                                <MenuItem x:Name="Open" Header="_Open" Height="50" HorizontalAlignment="Left" Width="250"/>
                                <MenuItem x:Name="Exit" Header="_Exit" Height="50" HorizontalAlignment="Left" Width="250"/>
                            </MenuItem>
                            <MenuItem Header="_Tools">
                                <MenuItem x:Name="Options" Header="O_ptions" Height="50" HorizontalAlignment="Left" Width="250"/>
                            </MenuItem>
                        </Menu>
                    </StackPanel>
    
                </Grid>
            ***</Viewbox>***
            <Grid.LayoutTransform>
                <ScaleTransform x:Name="ApplicationScaleTransform"
                            CenterX="0"
                            CenterY="0"
                            ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
                            ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
            </Grid.LayoutTransform>
    
    
        </Grid>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多