我有一個這樣的XAML
<Window x:Class="MainWindow"/span>
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:WPFPlayground"
mc:Ignorable="d"。
Title="MainWindow" Height="350"/span> Width="525"/span>>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/span>/>
<RowDefinition Height="80*"/span>/>
</Grid.RowDefinitions>
<Menu Grid.Row="0">/span>
<MenuItem
Background="Green"。
Header="First"/span>/>
<MenuItem
Background="Yellow"。
Header="Second"/span>/>
</Menu>/span>
</Grid>/span>
</Window>/span>
當我運行它時,我得到了這樣一個結果
我需要在Menu里面平均設定這些專案,比如第一個按鈕占50%,第二個也占50%,水平和垂直方向都是如此。而文本First和Second`也應該在按鈕的中心位置占據一個位置。
無論我做了什么都沒有用,我可以設定固定的高度或重量,但沒有辦法設定類似于
的東西。HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
它就是不作業
。P.S.我知道還有其他可能的方法,比如使用另一個視圖,但我需要使用Menu,它應該包含4或5個專案。
UPD
按照@BionicCode的回答,我得到的是這樣的結果
<Window x:Class="PlaygroundWPF.MainWindow"/span>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
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:PlaygroundWPF"。
mc:Ignorable="d"。
Title="MainWindow" Height="350"/span> Width="525"/span>>
<Window.Resources>
<Style TargetType="{x:Type Menu}"/span>>
< Setter Property="OverridesDefaultStyle" Value="True" />
< Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">/span>
<Setter.Value>/span>
<ControlTemplate TargetType="{x:Type Menu}"/span>>
<Border BorderThickness="1"/span>>/span>
<Border.BorderBrush>/span>
< LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
< GradientStop Color="{DynamicResource BorderLightColor}" Offset="0. 0"/span> />
< GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1. 0"/span> />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>/span>
<Border.Background>/span>
<LinearGradientBrush EndPoint="0。 5,1" StartPoint="0.5,0">
< GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
< GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1"/span> />
</LinearGradientBrush>
</Border.Background>>
<WrapPanel ItemWidth="200"/span>
ClipToBounds="True"/span>
IsItemsHost="True" />
</Border>
</ControlTemplate>/span>
</Setter.Value>/span>
</Setter>/span>
</Style>>
</Window.Resources>/span>
<Grid>/span>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/span>/>
<RowDefinition Height="20*"/span>/>
<RowDefinition Height="60*"/span>/>
</Grid.RowDefinitions>
<Menu Grid.Row="1"/span>>
<MenuItem
Background="Green"。
Header="First"/span>/>
<MenuItem
Background="Yellow"。
Header="Second"/span>/>
</Menu>/span>
</Grid>/span>
</Window>/span>
有三個問題
在這個例子中,我有兩個專案,它們應該在水平方向上同樣占據整個空間。UPD2
目前我的XAML實作是:
<Window x:Class="PlaygroundWPF.MainWindow"/span>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
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:PlaygroundWPF"。
mc:Ignorable="d"。
Title="MainWindow" Height="350"/span> Width="525"/span>>
<Window.Resources>
<Style TargetType="{x:Type Menu}"/span>>
< Setter Property="OverridesDefaultStyle" Value="True" />
< Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">/span>
<Setter.Value>/span>
<ControlTemplate TargetType="{x:Type Menu}"/span>>
<Border BorderThickness="1"/span>>/span>
<Border.BorderBrush>/span>
< LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
< GradientStop Color="{DynamicResource BorderLightColor}" Offset="0. 0"/span> />
< GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1. 0"/span> />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>/span>
<Border.Background>/span>
<LinearGradientBrush EndPoint="0。 5,1" StartPoint="0.5,0">
< GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
< GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1"/span> />
</LinearGradientBrush>
</Border.Background>>
<UniformGrid
ClipToBounds="True"
IsItemsHost="True" />
</Border>
</ControlTemplate>/span>
</Setter.Value>/span>
</Setter>/span>
</Style>>
</Window.Resources>/span>
<Grid>/span>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/span>/>
<RowDefinition Height="20*"/span>/>
<RowDefinition Height="60*"/span>/>
</Grid.RowDefinitions>
<Menu Grid.Row="1">/span>
<MenuItem HorizontalContentAlignment="Center"/span>
Background="Green"/span>
Header="Top Level Item"/>
<MenuItem HorizontalContentAlignment="Center"/span>
Background="Yellow"/span>
Header="Top Level Header"/span>>
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Item" />
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Header"> Header="Submenu Header">
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Item" />
</MenuItem>
</MenuItem>/span>
</Menu>/span>
</Grid>/span>
</Window>/span>
而且它看起來像這樣
BUT如果我再添加一個專案
- 希望的結果是
- 期望的結果是:該專案將像序列中的第三個專案一樣被添加到同一行,并平均占用空間 。
- 實際結果(見下面的截圖):該專案移到下一行 。
實際上,有兩個問題。第一個問題正如我所描述的,專案在下一行移動,第二個問題是專案本身的文本沒有居中(而是在左邊)。
這就是我從問題一開始就想解釋的問題。
uj5u.com熱心網友回復:
你必須覆寫Menu的默認模板并替換專案主機。
默認的主機是一個StackPanel,它總是為每個孩子分配最小的所需空間。
要自動均勻地分配可用的空間,你可以使用UniformGrid或WrapPanel來代替
下面是一些例子
下面的Style取自Microsoft Docs: 選單樣式和模板。但您可以使用XAML設計器或Blend輕松提取該樣式。
<Style TargetType="{x: Type Menu}">。
< Setter Property="OverridesDefaultStyle" Value="True" />
< Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">/span>
<Setter.Value>/span>
<ControlTemplate TargetType="{x:Type Menu}"/span>>
<Border BorderThickness="1"/span>>/span>
<Border.BorderBrush>/span>
< LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
< GradientStop Color="{DynamicResource BorderLightColor}" Offset="0. 0"/span> />
< GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1. 0"/span> />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>/span>
<Border.Background>/span>
<LinearGradientBrush EndPoint="0。 5,1" StartPoint="0.5,0">
< GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
< GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1"/span> />
</LinearGradientBrush>
</Border.Background>>
<WrapPanel ItemWidth="200"/span>
ClipToBounds="True"/span>
IsItemsHost="True" />
</Border>
</ControlTemplate>/span>
</Setter.Value>/span>
</Setter>/span>
</Style>
為了使MenuItem.Header的內容居中,你必須覆寫MenuItem的默認模板。在目標MenuItem(例如,Window.Resources或App.xaml)的范圍內的ResourceDictionary中添加以下模板。由于模板覆寫了資源ky,它們將自動應用。
注意,如果您的頂層MenuItem不包含子專案,您必須將模板的x:Key識別符號改為{x:Static MenuItem.TopLevelItemTemplateKey}。
將MenuItem內容居中的模式是在相應的專案級模板中找到托管的ContentPresenter,并通過將ContentPresenter.HorizontalAlignment系結到模板的父級MenuItem.HorizontalContentAlignment來修改它,例如通過使用TemplateBinding。
在定義了模板覆寫后,你只需使用MenuItem.HorizontalContentAlignment屬性將專案的內容居中。:
<ControlTemplate x:Key="{x:Static MenuItem. TopLevelHeaderTemplateKey}"/span>
TargetType="{x:Type MenuItem}">/span>
<Border x:Name="Border"
Background="{TemplateBinding Background}"/span>>
<Grid>/span>
<ContentPresenter Margin="6,3,6,3"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
ContentSource="Header"。
RecognizesAccessKey="True" />
<Popup x:Name="Popup"
Placement="Bottom"/span>
IsOpen="{TemplateBinding IsSubmenuOpen}"
AllowsTransparency="True"。
Focusable="False"/span>
PopupAnimation="Fade">
<Border x:Name="SubmenuBorder"/span>
SnapsToDevicePixels="True"。
BorderThickness="1"。
Background="{DynamicResource MenuPopupBrush}"/span>>
<Border.BorderBrush>/span>
<SolidColorBrush Color="{DynamicResource BorderMediumColor}"/span> />
</Border.BorderBrush>
<ScrollViewer CanContentScroll="True"/span>
>
<StackPanel IsItemsHost="True"/span>
KeyboardNavigation.DirectionalNavigation="Cycle" />
</ScrollViewer>
</Border>/span>
</Popup>/span>
</Grid>/span>
</Border>/span>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation"
Value="true">>
<Setter TargetName="Popup"
Property="PopupAnimation"/span>
Value="None"/span> />
</Trigger>
<Trigger Property="IsHighlighted"
Value="true">>
<Setter TargetName="Border"
屬性="BorderBrush"。
Value="Transparent" />
<Setter Property="Background" >。
TargetName="Border"/span>>
<Setter.Value>/span>
<LinearGradientBrush StartPoint="0,0"/span>
EndPoint="0,1">>
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{StaticResource ControlLightColor}"/span> />
<GradientStop Color="{StaticResource ControlMouseOverColor}"
Offset="1.0"/span> />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>/span>
</Setter>/span>
</Trigger>/span>
<Trigger SourceName="Popup"
Property="AllowsTransparency"
Value="True">>
<Setter TargetName="SubmenuBorder"
屬性="CornerRadius"。
Value="0,0,4,4"/>
<Setter TargetName="SubmenuBorder"
屬性="Padding"/span>
Value="0,0,0,3" />
</Trigger>
<Trigger Property="IsEnabled"/span>
Value="False">>
<Setter Property="Foreground">/span>
<Setter.Value>/span>
<SolidColorBrush Color="{StaticResource DisabledForegroundColor}"/span> />
</Setter.Value>
</Setter>/span>
</Trigger>/span>
</ControlTemplate.Triggers>
</ControlTemplate>/span>
舉例說明
選擇專案的MenuItem.Header屬性值是為了給你一個提示,你必須選擇哪個模板/樣式鍵。訪問Microsoft 檔案。Menu and MenuItem ControlTemplate Example,以找到所需的樣式和模板以及所需的x:Key識別符號。你可以修改它們(就像我做的那樣,用WrapPanel或UniformGrid替換Menu樣式中的StackPanel)。
將樣式和模板添加到選單范圍內的ResourceDictionary中(例如App.xaml)。
<Menu>
< MenuItem HorizontalContentAlignment="Center" Header="Top Level Item" />
< MenuItem HorizontalContentAlignment="Center" Header="Top Level Header">
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Item" />
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Header">
< MenuItem HorizontalContentAlignment="Center" Header="Submenu Item" />
</MenuItem>
</MenuItem>/span>
</Menu>/span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/312719.html
標籤:



