可以自己定义ItemsPanelTemplate:
<Menu Name="RootMenu">
<!-- 开始添加-->
<Menu.ItemsPanel >
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<!-- 结束添加-->
<MenuItem Name="MenuFile" Header="文件"/>
<MenuItem Name="MenuFileEdit" Header="编辑"/>
<MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>如果你指具体每个菜单,可以自订制Header:<MenuItem Name="MenuFile">
<MenuItem.Header>
<TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
</MenuItem.Header>
</MenuItem>
<Menu Name="RootMenu">
<!-- 开始添加-->
<Menu.ItemsPanel >
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<!-- 结束添加-->
<MenuItem Name="MenuFile" Header="文件"/>
<MenuItem Name="MenuFileEdit" Header="编辑"/>
<MenuItem Name="MenuHelps" Header="帮助"/>
</Menu>如果你指具体每个菜单,可以自订制Header:<MenuItem Name="MenuFile">
<MenuItem.Header>
<TextBlock Background="Yellow" Width="200" TextAlignment="Center" Text="文件" />
</MenuItem.Header>
</MenuItem>
<MenuItem Name="MenuFile">
<MenuItem.Template>
<ControlTemplate TargetType="MenuItem">
<TextBlock x:Name="txt" Padding="2,2,2,2" Text="文件" Background="White"></TextBlock>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="txt" Property="Background" Value="Blue"></Setter>
<Setter TargetName="txt" Property="Padding" Value="2,2,2,2"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
<MenuItem Name="MenuFileEdit"/>
<MenuItem Name="MenuHelps"/>
</Menu>
VerticalAlignment="Center"
TextBlock.TextAlignment="Center" Padding="5"
第二种似乎不显示文本了?//这堆我都试过了..
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;
//我是这种怎么办?
<Menu Name="RootMenu">
<MenuItem Name="MenuFile" Header="文件"/>
<MenuItem Name="MenuFileEdit"/>
<MenuItem Name="MenuHelps"/>
</Menu>
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center;
//这堆我都试过了!!
This.HorizontalAlignment = HorizontalAlignment.Center;
This.VerticalAlignment = VerticalAlignment.Center;
This.HorizontalContentAlignment = HorizontalAlignment.Center;
This.VerticalContentAlignment = VerticalAlignment.Center; <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="60"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<DockPanel>
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" Margin="6,0,6,0" VerticalAlignment="Center"/>
<Path x:Name="GlyphPanel" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Visibility="Collapsed" VerticalAlignment="Center"/>
<!--显示内容-->
<Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
<TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>引用:<MenuItem Header="步骤2" Width="80" Height="30" Style="{StaticResource myItemStyle}"></MenuItem>
咦,怎么回事?设置Header了没有
这应该设置了吧?!
对啊。.net framework 版本是4.0的,重新编译试试
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!
Fill="{TemplateBinding Foreground}" 这句没问题
快要炸毛了!!
知道哪里错了,手误,把ControlTemplate 改成 <ControlTemplate TargetType="{x:Type MenuItem}">
<Window.Resources>
<Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Menu Name="RootMenu">
<MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
<Window.Resources>
<Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Menu Name="RootMenu">
<MenuItem Name="MenuFlie" Style="{StaticResource myItemStyle}"></MenuItem>
</Menu>MenuFile.Header = 文件;
MenuFile.Width = 60;
MenuFile.Height = 30;
MenuFile.FontWeight = FontWeights.Thin;
MenuFile.Foreground = new SolidColorBrush(Color.FromRgb(0xF1, 0xF1, 0xF1));
MenuFile.MouseEnter += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(135, 28, 134, 238));
MenuFile.MouseLeave += (sender, e) => MenuFile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
参见:http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.triggers(v=vs.110).aspx
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
还是不行。。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<TextBlock Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
还是不行。。
用 ControlTemplate Trigger <Style x:Key="myItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="60"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="B_MenuItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1">
<TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="B_MenuItem" Value="Yellow"/>
<Setter Property="Foreground" TargetName="mytb" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最后请教下大神 它还有生育能力 还可以生孩纸麽?
为毛2Menu作为“文件”的下级菜单 却不显示呢。。
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="60"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<DockPanel>
<Border x:Name="B_MenuItem" Width="{TemplateBinding Width}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="1">
<TextBlock x:Name="mytb" Text="{TemplateBinding Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" Width="{TemplateBinding Width}"
IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
Placement="Bottom" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MaxHeight="150">
<Border x:Name="B" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<!--阴影矩形-->
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}"/>
<Border BorderBrush="Brown" BorderThickness="1" Background="DarkGray" HorizontalAlignment="Stretch" Width="{TemplateBinding Width}">
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" KeyboardNavigation.TabNavigation="Cycle"/>
</Border>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="B_MenuItem" Value="Yellow"/>
<Setter Property="Foreground" TargetName="mytb" Value="Black"/>
</Trigger>
<Trigger Property="IsSuspendingPopupAnimation" Value="True">
<Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
</Trigger>
<Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True">
<Setter Property="Background" TargetName="B" Value="Yellow"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
<Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
<Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
他孙子不像亲生的
是給二级菜单写个样式 定义二级菜单的Popup?
他孙子不像亲生的
是給二级菜单写个样式 定义二级菜单的Popup?
孙子也一样引用。
那你再从新写个MenuItem样式,专门用于孙子项的引用。
所以我尝试了給儿子套用样式 然后就成了这样。。
我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。
所以我尝试了給儿子套用样式 然后就成了这样。。
我在纠结 横向的是Popup麽?
我知道应该滚去看书 可是书还没到。。
或者你先学会使用Blend