【问题标题】:Center MenuItem Content within strechted Menu拉伸菜单中的中心菜单项内容
【发布时间】:2023-04-02 19:55:01
【问题描述】:

我想在 MenuHeader 中将 Stackpanel 居中。 MenuItems 在整个菜单上均等地伸展。 我尝试将 Stackpanel Width 绑定到 Parent MenuItem Width,这在 Designer 中得到了完美的结果。但是,我在调试的时候,Stackpanel 还是靠左对齐的。

<Menu Grid.Column="1"
              Grid.Row="1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="1"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <MenuItem x:Name="OpenSourceView"                       
                      Background="{DynamicResource PrimaryHueMidBrush}"  
                      HorizontalContentAlignment="Center"
                      Padding="0"
                      Click="OpenMenu"  
                      Height="60"                         
                     >
            <MenuItem.Header>
                <StackPanel     Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                >
                    <materialDesign:PackIcon 
                            Kind="Camcorder" 
                            Height="30" 
                            Width="30"
                            Foreground="#262526"
                            HorizontalAlignment="Center"></materialDesign:PackIcon>
                    <AccessText Foreground="#262526" 
                                    FontWeight="Bold" 
                                    FontSize="12" 
                                    Width="Auto"
                                    VerticalAlignment="Center"
                                HorizontalAlignment="Center">Source</AccessText>
                </StackPanel>
            </MenuItem.Header>
        </MenuItem>
</Menu>

它在设计器中的外观以及我想要的效果:

https://i.imgur.com/2nkHxVw.png

调试时的样子:

https://i.imgur.com/ib1ILig.png

编辑: 我用网格替换了 Stackpanel。现在它居中但被截断了一半:

https://i.imgur.com/0piRPcM.png

<Menu Grid.Column="1"
              Grid.Row="1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="1"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <MenuItem x:Name="OpenSourceView"                       
                      Background="{DynamicResource PrimaryHueMidBrush}"                       
                      Padding="0"
                      Click="OpenMenu"  
                      Height="60"                         
                     >
            <MenuItem.Header>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="30"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"/>
                    </Grid.ColumnDefinitions>

                    <materialDesign:PackIcon 
                            Grid.Column="0"
                            Grid.Row="0"
                            Kind="Camcorder" 
                            Height="30" 
                            Width="30"
                            Foreground="#262526"
                            HorizontalAlignment="Center"/>
                    <AccessText Foreground="#262526"
                                Grid.Row="1"
                                 FontWeight="Bold" 
                                 FontSize="12" 
                                 Width="Auto"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center">Source</AccessText>
                </Grid>
            </MenuItem.Header>
        </MenuItem>
</Menu>

【问题讨论】:

  • StackPanel 是一个容器,其中所有子元素都放在堆栈中,也就是说一个接一个。我建议使用一列两行的网格。
  • @LPGTE.SOFTS 感谢您的回答。我试着听从你的建议,并用更新的代码编辑了我的问题。它仍然无法正常工作。

标签: wpf xaml menu


【解决方案1】:

view的整个代码没有显示出来,这个bug可能是返回到Menu的定义。

<Menu Grid.Column = "1" Grid.Row = "1">

这是您提出的菜单的一个实现,其中菜单与 StackPanel 和 Grid 很好地居中。
将控件materialDesign替换为按钮进行测试。

堆栈面板

<Window
        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"
        mc:Ignorable="d"
        Title="MainWindow"  Width="1137.333"
        WindowState="Maximized"
        WindowStartupLocation="CenterScreen">

    <DockPanel>

        <!-- Menu Area -->
        <Border VerticalAlignment="Top" DockPanel.Dock="Top" Margin="0,5,0,5">

            <!-- Diagram Area -->
            <Grid x:Name="DiagramPane" Margin="10,0,10,10">

                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />                  
                </Grid.RowDefinitions>

                <Menu VerticalAlignment="Top" Grid.Row="0" Height="60" Margin="0,-7,0,0" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Rows="1"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <MenuItem x:Name="OpenSourceView"                       
                      Background="Yellow"  
                      HorizontalContentAlignment="Center"
                      Padding="0"
                      Height="60" >

                        <MenuItem.Header>
                            <StackPanel     Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"                                >

                                <Button Grid.Column="0"   HorizontalAlignment="Center" Height="30" Width="30" Margin="0,0,0,0"  IsDefault="True"   Command = "{Binding AjoutCommand}" CommandParameter = "null">
                                    <Image Opacity="0.4" HorizontalAlignment="Center"  VerticalAlignment="Center" Source="/Images/AddFile.png" Grid.Column="2"  Height="16"  />
                                </Button>

                                <AccessText Foreground="#262526" 
                                    FontWeight="Bold" 
                                    FontSize="12" 
                                    Width="Auto"
                                    VerticalAlignment="Center"
                                HorizontalAlignment="Center">Source</AccessText>
                            </StackPanel>
                        </MenuItem.Header>
                    </MenuItem>
                </Menu>

            </Grid>
        </Border>
    </DockPanel>
</Window>

图片

网格

<Window
        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"
        mc:Ignorable="d"
        Title="MainWindow"  Width="1137.333"
        WindowState="Maximized"
        WindowStartupLocation="CenterScreen">

    <DockPanel>
        <!-- Menu Area -->
        <Border VerticalAlignment="Top" DockPanel.Dock="Top" Margin="0,5,0,5">
            <!-- Diagram Area -->
            <Grid x:Name="DiagramPane" Margin="10,0,10,10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <Menu VerticalAlignment="Top" Grid.Row="0" Height="60" Margin="0,-7,0,0" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Rows="1"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <MenuItem x:Name="OpenSourceView"                       
                      Background="Yellow"                       
                      Padding="0"
                      Height="60"                         
                     >
                        <MenuItem.Header>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="30"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"/>
                                </Grid.ColumnDefinitions>

                                <Button Grid.Column="0"   HorizontalAlignment="Center" Height="30" Width="30" Margin="0,0,0,0"  IsDefault="True"   Command = "{Binding AjoutCommand}" CommandParameter = "null">
                                    <Image Opacity="0.4" HorizontalAlignment="Center"  VerticalAlignment="Center" Source="/Images/AddFile.png" Grid.Column="2"  Height="16"  />
                                </Button>

                            <AccessText Foreground="#262526"
                                Grid.Row="1"
                                 FontWeight="Bold" 
                                 FontSize="12" 
                                 Width="Auto"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center">Source</AccessText>
                            </Grid>
                        </MenuItem.Header>
                    </MenuItem>
                </Menu>               
            </Grid>
        </Border>

    </DockPanel>

</Window>

图片

真诚的

【讨论】:

  • 谢谢,我得出的结论是,我无法通过菜单控件实现我的目标,因为我希望菜单项被拉伸而不仅仅是居中。我将其替换为内部带有按钮的网格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 2017-12-14
  • 2017-06-04
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多