wpf-controls 相关问题

WPF控件包括UserControls,它是其他控件的复合集合,以及CustomControls,它们是使用WPF样式和模板构建的控件。

WPF ListBox.SelectedItems 作为 ListBoxItem

有没有办法在WPF中的SelectionChanged(在ListBox上)事件中将ListBox的选定项作为ListBoxItem? 现在,当我调用 listBox.SelectedItems 时,我会得到项目列表...

回答 2 投票 0

WPF - 如何设置菜单控件的样式以删除左边距?

我已将默认菜单控件添加到我的用户控件中。我需要设置菜单样式以删除包含图标或复选框空间的左边距。我怎样才能做到这一点? XAML: ... 我已将默认菜单控件添加到我的用户控件中。我需要设置菜单样式以删除包含图标或复选框空间的左边距。我该怎么做? XAML: <Menu> <MenuItem Header="MyMenu" FontSize="10"> <MenuItem Header="Options..." /> <MenuItem Header="About" /> </MenuItem> </Menu> 它目前的渲染方式与任何其他开箱即用的菜单控件一样: 我不希望菜单项左侧有边距或列。这通常用于图标等。 我认为这就是你所追求的(再次,使用Expression Blend来解决它,但就它显示的内容而言,它是我能得到的最简约的......并且花了很多时间来玩弄).. .您可以将以下内容放入空白 WPF 应用程序中作为示例: <Window x:Class="MenuItemWithNoIcon.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <SolidColorBrush x:Key="MenuItem.Highlight.Background" Color="#3D26A0DA"/> <SolidColorBrush x:Key="MenuItem.Highlight.Border" Color="#FF26A0DA"/> <SolidColorBrush x:Key="Menu.Disabled.Foreground" Color="#FF707070"/> <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Background" Color="#0A000000"/> <SolidColorBrush x:Key="MenuItem.Highlight.Disabled.Border" Color="#21000000"/> <SolidColorBrush x:Key="MenuItem.Selected.Border" Color="#FF26A0DA"/> <SolidColorBrush x:Key="MenuItem.Selected.Background" Color="#3D26A0DA"/> <Geometry x:Key="Checkmark">F1 M 10.0,1.2 L 4.7,9.1 L 4.5,9.1 L 0,5.2 L 1.3,3.5 L 4.3,6.1L 8.3,0 L 10.0,1.2 Z</Geometry> <SolidColorBrush x:Key="Menu.Static.Foreground" Color="#FF212121"/> <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="22" SnapsToDevicePixels="true"> <Grid Margin="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <ContentPresenter x:Name="menuHeaderContainer" ContentSource="Header" HorizontalAlignment="Stretch" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Stretch"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="Icon" Value="{x:Null}"/> <Trigger Property="IsChecked" Value="True"/> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsHighlighted" Value="True"/> <Condition Property="IsEnabled" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Background}"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Border}"/> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> <SolidColorBrush x:Key="Menu.Static.Border" Color="#FF999999"/> <SolidColorBrush x:Key="Menu.Static.Background" Color="#FFF0F0F0"/> <SolidColorBrush x:Key="Menu.Static.Separator" Color="#FFD7D7D7"/> <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry> <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}"> <Setter Property="ClickMode" Value="Hover"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RepeatButton}"> <Border x:Name="templateRoot" BorderBrush="Transparent" BorderThickness="1" Background="Transparent" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="Center" Margin="6" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/> <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry> <Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}"> <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/> <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid SnapsToDevicePixels="true"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Border Grid.Column="0" Grid.Row="1"> <ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" Margin="{TemplateBinding Padding}"/> </Border> <RepeatButton Grid.Column="0" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Command="{x:Static ScrollBar.LineUpCommand}" Focusable="false" Grid.Row="0" Style="{StaticResource MenuScrollButton}"> <RepeatButton.Visibility> <MultiBinding ConverterParameter="0" Converter="{StaticResource MenuScrollingVisibilityConverter}" FallbackValue="Visibility.Collapsed"> <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/> </MultiBinding> </RepeatButton.Visibility> <Path Data="{StaticResource UpArrow}" Fill="{StaticResource Menu.Static.Foreground}"/> </RepeatButton> <RepeatButton Grid.Column="0" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Command="{x:Static ScrollBar.LineDownCommand}" Focusable="false" Grid.Row="2" Style="{StaticResource MenuScrollButton}"> <RepeatButton.Visibility> <MultiBinding ConverterParameter="100" Converter="{StaticResource MenuScrollingVisibilityConverter}" FallbackValue="Visibility.Collapsed"> <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/> <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/> </MultiBinding> </RepeatButton.Visibility> <Path Data="{StaticResource DownArrow}" Fill="{StaticResource Menu.Static.Foreground}"/> </RepeatButton> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <Grid VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/> <ContentPresenter Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" PlacementTarget="{Binding ElementName=templateRoot}"> <Border x:Name="SubMenuBorder" BorderBrush="{StaticResource Menu.Static.Border}" BorderThickness="1" Background="{StaticResource Menu.Static.Background}" Padding="0"> <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"> <Grid RenderOptions.ClearTypeHint="Enabled"> <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/> </Canvas> <Rectangle Fill="{StaticResource Menu.Static.Separator}" HorizontalAlignment="Left" Margin="0" Width="1"/> <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/> </Grid> </ScrollViewer> </Border> </Popup> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSuspendingPopupAnimation" Value="true"> <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/> </Trigger> <Trigger Property="Icon" Value="{x:Null}"/> <Trigger Property="IsChecked" Value="true"> <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> </Trigger> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> <Setter Property="Fill" TargetName="GlyphPanel" Value="{StaticResource Menu.Disabled.Foreground}"/> </Trigger> <Trigger Property="ScrollViewer.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> <ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <Grid VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <ContentPresenter x:Name="Icon" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/> <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{StaticResource Menu.Static.Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/> <ContentPresenter Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="Icon" Value="{x:Null}"> <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> </Trigger> <Trigger Property="IsChecked" Value="true"> <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> </Trigger> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Background}"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> <Setter Property="Fill" TargetName="GlyphPanel" Value="{StaticResource Menu.Disabled.Foreground}"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsHighlighted" Value="True"/> <Condition Property="IsEnabled" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Background" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Background}"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Disabled.Border}"/> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry> <ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="22" SnapsToDevicePixels="true"> <Grid Margin="-1"> <Grid.ColumnDefinitions> <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/> <ColumnDefinition Width="13"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="30"/> <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <ContentPresenter x:Name="Icon" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/> <Border x:Name="GlyphPanel" BorderBrush="{StaticResource MenuItem.Highlight.Border}" BorderThickness="1" Background="{StaticResource MenuItem.Highlight.Background}" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22"> <Path x:Name="Glyph" Data="{DynamicResource Checkmark}" Fill="{StaticResource Menu.Static.Foreground}" FlowDirection="LeftToRight" Height="11" Width="9"/> </Border> <ContentPresenter Grid.Column="2" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/> <TextBlock Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/> <Path x:Name="RightArrow" Grid.Column="5" Data="{StaticResource RightArrow}" Fill="{StaticResource Menu.Static.Foreground}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/> <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="-2" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Right" VerticalOffset="-3"> <Border x:Name="SubMenuBorder" BorderBrush="{StaticResource Menu.Static.Border}" BorderThickness="1" Background="{StaticResource Menu.Static.Background}" Padding="2"> <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"> <Grid RenderOptions.ClearTypeHint="Enabled"> <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/> </Canvas> <Rectangle Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/> <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/> </Grid> </ScrollViewer> </Border> </Popup> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSuspendingPopupAnimation" Value="true"> <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/> </Trigger> <Trigger Property="Icon" Value="{x:Null}"> <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> </Trigger> <Trigger Property="IsChecked" Value="True"> <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> </Trigger> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="templateRoot" Value="Transparent"/> <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/> <Setter Property="Fill" TargetName="Glyph" Value="{StaticResource Menu.Disabled.Foreground}"/> <Setter Property="Fill" TargetName="RightArrow" Value="{StaticResource Menu.Disabled.Foreground}"/> </Trigger> <Trigger Property="ScrollViewer.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> <Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}"> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="ScrollViewer.PanningMode" Value="Both"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> <Style.Triggers> <Trigger Property="Role" Value="TopLevelHeader"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/> <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> <Setter Property="Padding" Value="6,0"/> </Trigger> <Trigger Property="Role" Value="TopLevelItem"> <Setter Property="Background" Value="{StaticResource Menu.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Border}"/> <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/> <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> <Setter Property="Padding" Value="6,0"/> </Trigger> <Trigger Property="Role" Value="SubmenuHeader"> <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Menu> <MenuItem Header="File" Style="{DynamicResource MenuItemStyle1}"> <MenuItem Header="Exit" Style="{DynamicResource MenuItemStyle1}"/> </MenuItem> </Menu> </Grid> </Window> 简单快捷的方法如下。创建 ItemsPanelTemplate 资源: <ItemsPanelTemplate x:Key="MenuItemPanelTemplate"> <StackPanel Background="White"/> </ItemsPanelTemplate> 将以下MenuItem样式添加到资源中,就完成了。 <Style TargetType="{x:Type MenuItem}"> <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/> </Style> 要将相同的 Style 应用于 ContextMenu,您需要再创建一个 Style,如下所示: <Style TargetType="{x:Type ContextMenu}"> <Setter Property="ItemsPanel" Value="{StaticResource MenuItemPanelTemplate}"/> </Style> 在上下文菜单之上,您还必须添加: <ContextMenu ItemsSource="{Binding MyItems}" > <ContextMenu.ItemTemplate> <DataTemplate> <TextBlock Margin="-20,0,-40,0" Text="{Binding Name}"/> </DataTemplate> </ContextMenu.ItemTemplate> </ContextMenu> 因此它将覆盖图标空间并显示大小写文本块。这是最简单、最容易的解决方案。 这不是很直接,但您需要创建一个 MenuItemStyle,最简单的方法是通过 Expression Blend: <Menu> <MenuItem Header="MyMenu" Style="{DynamicResource MenuItemStyle1}"> <MenuItem Header="Options..." /> <MenuItem Header="About" /> </MenuItem> </Menu> 它创建了一组极其冗长的模板和样式,您需要编辑菜单项以删除网格的固定宽度第一列,然后在 SubMenuBorder ContentControl 模板中删除形成背景阴影的矩形。我附上了一个已删除边距的示例项目。 在此处下载示例项目。 这里有两个选项: 简短、简单、直接。将 ItemsPanelTemplate 设置为 MenuItem 或 ContextMenu,具体取决于您使用的菜单类型(请参阅详细信息)。 激进。从头开始重写 Menu 风格。有两种即用型: 来自 MahApps.Metro 的XAML 风格的Menu(普通菜单和ContextMenu) Jeff Wilcox 的风格启发了 MahApps 的前一个风格(link) 我的简单方法是在 ItemTemplate 中为网格使用负边距 <ContextMenu.ItemTemplate> <DataTemplate> <Grid Margin="-20,0,-40,0"><!--this part is important--> <Grid.ColumnDefinitions> <ColumnDefinition Width="20"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding Ident}"/> <TextBlock Grid.Column="1" Text="{Binding Description}"/> </Grid> </DataTemplate> </ContextMenu.ItemTemplate> 查看完整答案这里 我正在使用 WPF Notifyicon(硬编码)并使用以下代码删除了菜单的图标部分: <Window ...> <Window.Resources> <ItemsPanelTemplate x:Key="MenuTPL"> <StackPanel Margin="-30,0,0,0" Background="White"/> </ItemsPanelTemplate> </Window.Resources> <Grid> ... <ContextMenu> <ContextMenu.Style> <Style TargetType="{x:Type ContextMenu}"> <Setter Property="ItemsPanel" Value="{StaticResource MenuTPL}"/> </Style> </ContextMenu.Style> <MenuItem Header="Exit" Click="Exit_MenuItemClick"/> </ContextMenu> ... </Grid> </Window> 要删除空格并且从不使用图标,您必须更改 MenuItem.SubmenuItemTemplateKey 的模板或 MenuItem 的模板。 如果您只需要摆脱垂直线并继续使用图标空间,请遵循此答案。 带有网格的 Windows 有我的 CustomContextMenu.xaml 作为网格资源: <Window ...> <Grid> <Grid.Resources> <ResourceDictionary Source="CustomContextMenu.xaml"/> </Grid.Resources> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Menu item 1" > <MenuItem Header="Menu item 2" > <MenuItem.Icon> <Image Source="icon.jpg"/> </MenuItem.Icon> </MenuItem> </MenuItem> <Separator Style="{StaticResource MySeparatorStyle}" /> <MenuItem IsEnabled="False" Header="Menu item 3" /> </ContextMenu> </Grid.ContextMenu> <TextBlock>test</TextBlock> </Grid> </Window> 这是我的 CustomContextMenu.xaml,它有一个 CustomSeparatorStyle 模板,可将分隔线延伸到上下文菜单的左边距。还有一个 ContextMenu 模板来隐藏垂直线。 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!-- Outer menu --> <Style TargetType="{x:Type ContextMenu}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="MaxWidth" Value="295" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ContextMenu}"> <!-- Here is where you change the border thickness to zero on the menu --> <Border x:Name="Border" Background="#CCCCC7" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="0"> <Border.Effect> <DropShadowEffect Direction="135" Opacity=".8" ShadowDepth="2" Color="Black" /> </Border.Effect> <StackPanel ClipToBounds="True" IsItemsHost="True" Orientation="Vertical" /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="Border" Property="Background" Value="#F7F7F4" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- Separator --> <Style x:Key="CustomSeparatorStyle" TargetType="{x:Type Separator}"> <Setter Property="Height" Value="1" /> <Setter Property="Margin" Value="-30,5,0,5" /> <Setter Property="Background" Value="#F7F7F4" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Separator}"> <Border BorderBrush="#DADAD6" BorderThickness="1" /> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> 右侧菜单是使用上面的代码创建的。您可以注意到大小和阴影的差异。为了保留原始菜单的阴影,您必须排除 Border.Effect。 使用 RadMenuGroupItem。 RadMenuGroupItem 继承自 RadMenuItem 类,它用作 RadMenuItem 下拉列表的容器。换句话说,任何 UI 元素都可以放置在 RadMenuGroupItem 内部。默认情况下,RadMenuGroupItem 的背景颜色为白色,并且没有与 RadMenuItem 不同颜色的图标区域,因此您可以轻松地在下拉列表中使用不同大小的图标。除此之外,RadMenuGroupItem 还有一个 Header 属性,该属性显示在所有组项的顶部。 <telerik:RadMenu VerticalAlignment="Top"> <telerik:RadMenuItem Header="Shapes" /> <telerik:RadMenuItem Header="Sizes"> <telerik:RadMenuGroupItem Header="Header"> <telerik:RadMenuItem Header="Small" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="35" Height="35" /> <telerik:RadMenuItem Header="Medium" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="45" Height="45" /> <telerik:RadMenuItem Header="Large" IconTemplate="{StaticResource IconTemplate}" IconColumnWidth="55" Height="55" /> </telerik:RadMenuGroupItem> </telerik:RadMenuItem> 这就是结果: 感谢您的成功想法。对于 .net Framework 4.5 和 VS 2012,我相应地为 ContextMenu 和 MenuItem 编写了: private const double ICON_SIZE = 32; void ContextMenu_Loaded(object sender, System.Windows.RoutedEventArgs e) { if (_pointerControl.ContextMenu.Template != null) { System.Windows.Shapes.Rectangle r1 = _pointerControl.ContextMenu.Template.FindName("3_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; System.Windows.Shapes.Rectangle r2 = _pointerControl.ContextMenu.Template.FindName("4_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; System.Windows.Shapes.Rectangle r3 = _pointerControl.ContextMenu.Template.FindName("5_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle; double width = Math.Max(28, ICON_SIZE+14); r1.Width = width; r2.Margin = new System.Windows.Thickness(width + 1, 2, 0, 2); r3.Margin = new System.Windows.Thickness(width + 2, 2, 0, 2); } } void mi_Loaded(object sender, System.Windows.RoutedEventArgs e) { System.Windows.Controls.MenuItem mi = sender as System.Windows.Controls.MenuItem; if (mi != null && mi.Template != null) { System.Windows.Controls.ContentPresenter cp = mi.Template.FindName("Icon", mi) as System.Windows.Controls.ContentPresenter; cp.Height = ICON_SIZE + 6; cp.Width = ICON_SIZE + 6; } }

回答 9 投票 0

嵌套选项卡中的 WPF Ctrl+Tab 功能 [已关闭]

在 WPF 中,当我使用 Ctrl+Tab 在 TabControl 的选项卡之间切换时,一切按预期工作,在每个选项卡之间切换。但是,当父选项卡内有嵌套选项卡时,这些嵌套选项卡

回答 1 投票 0

如何优化IntializeComponent()

我有一个wpf窗口,其中有ScrollViewer和TextBlock,当用户打开此窗口时,我们从服务器获取1000个AT对象并添加到scrollviewer中的该窗口上。当点击特定项目时...

回答 1 投票 0

C# - 尝试将文件路径添加到 NavigateUri 中,每个用户可能会有所不同

我正在尝试将文件路径超链接添加到我的 WPF 项目中,该项目的一部分可能对每个用户来说都不同。我知道一个注册表位置,我可以在其中获取...的第一部分...

回答 1 投票 0

在WPF中当我尝试使用CTRL+TAB切换选项卡时

在 WPF 中,当我尝试使用 CTRL+TAB 切换选项卡时,它可以正常工作,但是如果选项卡位于父选项卡内,那么它将切换到子选项卡,只需要移动父选项卡? 我已经尝试过...

回答 1 投票 0

如何在自定义组合框控件的下拉列表末尾添加“AddNew”按钮?

我有一个带有组合框的自定义控件。我想在下拉列表的末尾添加一个按钮,上面写着“AddNew”,例如“AddNewStudent”,单击该按钮时,会打开一个表单

回答 1 投票 0

在WPF中,为什么ListBox是白色的?

我正在尝试在 WPF 中定义应用程序范围的主题。我首先将默认窗口背景设置为黑色。在我的全局资源中(当前在 App.xaml 中),我设置了以下内容 我正在尝试在 WPF 中定义一个应用程序范围的主题。我首先将默认窗口背景设置为黑色。在我的全局资源中(当前在 App.xaml 中),我设置了以下内容 <Application x:Class="f12dotnet.ThemeSandbox.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#444" /> </Application.Resources> </Application> 我的用户界面中有一个 ListBox,它仍然是白色的。将 SystemColors.ControlBrushKey 设置为透明没有帮助。 在默认列表框模板中,我在最顶部看到一个设置器,将Background属性(如我所期望的)设置为SystemColors.WindowBrushKey。尽管如此,背景还是白色的。 <Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 使用 DependencyPropertyHelper.GetValueSource 告诉我来源是 DefaultStyle,这意味着,根据 MSDN: 源来自默认样式的setter。默认样式来自当前主题。 那么那是哪里? 在 ListBox 模板的下方,我注意到以下内容: <!-- [[Aero2.NormalColor]] --> <SolidColorBrush x:Key="ListBox.Static.Background" Color="#FFFFFFFF" /> <SolidColorBrush x:Key="ListBox.Static.Border" Color="#FFABADB3" /> 我怀疑这就是罪魁祸首,因为出于绝望,我实际上在 WPF GitHub 存储库中搜索了“#FFFFFF”,除此之外没有发现任何有趣的东西。有趣的是,它是在控件模板中定义的,而不是在经典主题 XAML 中。 如果这是值的来源,我可以像覆盖SystemColors.WindowBrushKey一样“覆盖”此画笔吗? 有什么方法我还没有想到吗? 或者 正在为 ListBox 创建默认样式(谁知道还有多少个控件),将背景设置为透明,我唯一的选择? PS:此时,我意识到即使我确实覆盖了ListBox.Static.Background画笔,我仍然必须对许多其他控件执行相同的操作,因此创建默认样式(从一个基本样式继承)实际上似乎是更好的方法。 我只是好奇,深入兔子洞,想查明白色背景的起源并在那里“阻止它”。 此外,显然有人“十年前遇到过类似的问题”,并设法按照我想要的方式解决它并期望它起作用。但它对我、今天或 Windows 11 不起作用。 我建议您检查您的元素中实际使用了哪个模板。我怀疑这将是您提供链接的模板。该模板使用“Value =”{DynamicResource”。据我所知,在 Windows 8 之前,这是有效的。并且具有覆盖系统密钥的解决方案在 Windows 7 中完美地完成了这项工作。 现在不行了。因此,在大多数情况下,画笔是在指定模板的同一位置指定的。并且使用“StaticResource”来链接到它。这意味着在模板之外不能高估它。 您可以通过以下方式获取配置中的元素使用的模板: 在 Studio 设计器中,打开包含所需元素的 XAML(通常为此,我会创建一个包含一个元素的新测试空窗口); 在设计器(视觉形式)中,右键单击上下文菜单; 在其中选择“编辑模板”->“编辑副本”; 之后将显示一个带有参数的对话框 - 设置您需要的参数; 单击“确定”,您将收到您的元素使用的模板。 在我的例子中,这是通过指定方式获得的ListBox模板的样子: <SolidColorBrush x:Key="ListBox.Static.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ListBox.Static.Border" Color="#FFABADB3"/> <SolidColorBrush x:Key="ListBox.Disabled.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ListBox.Disabled.Border" Color="#FFD9D9D9"/> <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}"> <Setter Property="Background" Value="{StaticResource ListBox.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource ListBox.Static.Border}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="Both"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBox}"> <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1" SnapsToDevicePixels="true"> <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </ScrollViewer> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Background" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Background}"/> <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ListBox.Disabled.Border}"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsGrouping" Value="true"/> <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/> </MultiTrigger.Conditions> <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

回答 1 投票 0

如何在WPF中设置DataGrid的数据源?

我需要将数据库中的表设置为 WPF 中 GridGrid 的数据源。在 Windows 窗体中,该属性称为 DataSource,但在 WPF 中不存在这样的属性,那么我该怎么做呢?

回答 5 投票 0

以编程方式设置的图像在运行时不显示

我正在为 DataTemplate 创建一个自定义 UserControl,它接受元数据对象并将 FilePath 分配给图像(这些路径由用户在运行时定义)。然而,尽管扔...

回答 1 投票 0

有人成功在 C#“Visual Studio”VSIX 扩展项目的 ToolWindow 中托管 WebView2 控件吗?

我有一个 Visual Studio VSIX 项目,当前使用非常过时的基于 IE 的 WebBrowser 控件。微软似乎不希望我们再使用该控件,因为他们已经弃用了我......

回答 1 投票 0

在powershell中的datagridview控件中右键选择一行

我正在 Powershell 中开发一个使用 WPF 控件的项目。 我有一个 datagridview,其中只能选择一整行。还有一个上下文菜单条在数据网格视图中工作正常......

回答 2 投票 0

如何以编程方式将 RibbonTab 添加到 WPF 功能区(2010 年 10 月版本)?

有人知道如何以编程方式将 RibbonTab 添加到 Mircosoft WPF 功能区吗?我找不到示例或至少找不到在文档或 Ribbon 智能感知中使用的属性。 我...

回答 3 投票 0

如何链接/关联 Syncfusion-DockingManager 的两个子级?

我使用 Syncfusion-DockingManager 将 UserControl 显示为 ContentControl 的内容。可以打开属性 UserControl(也作为 ContentControl 的内容),它显示与...相关的数据。

回答 1 投票 0

如何正确处理焦点

假设我有下一个布局: 窗户 用户控制 用户控制 用户控制 按钮 网格控制 网格单元 假设 GridCell 当前具有键盘焦点。 ...

回答 2 投票 0

wpf中如何用MVVM处理ComboBox的SelectionChanged事件?

如何使用 MVVM 模式引发/处理 WPF 的 ComboBox 的 SelectionChanged 事件? 请详细解释一下,我是 WPF 新手。 我想要的是,当 ComboBox 项目

回答 6 投票 0

WPF - 拉伸图像以适合区域

我试图根据图像的形状(蓝色块)来显示以下内容: 我有一些问题: 图像拉伸应限制在其可用区域内,由

回答 1 投票 0

与 ScreenSpaceGroup3D 的行为略有不同

我在sharpDX中为我的模型使用ScreenSPceGroup3D类,因为我需要独立于缩放始终相同的大小。与相机位置的改变无关,模型始终可见(旋转...

回答 2 投票 0

WPF - 为什么 Keyboard.Focus() 不起作用?

TabItem 控件上有一个 TextBox 项 (MyTextBox)。我的代码如下所示: MyTextBox.Focus(); 键盘.焦点(MyTextBox); 当我通过调试器运行此代码时,我看到以下内容

回答 7 投票 0

在最小的 WPF 应用程序中主窗口不会占据屏幕的右半部分

我希望在启动 WPF 应用程序时让主应用程序窗口占据显示器的一半。如果我使用多个显示器,我希望窗口占据其中最后一个......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.