删除上下文菜单上的图标空间

发布于 2024-10-13 08:59:25 字数 456 浏览 1 评论 0原文

我将以下 ContextMenu 连接到列表框。我真的想删除图标所在的空间。这可以通过某种简单的方式实现吗?

                    <ListBox.ContextMenu>
                    <ContextMenu >
                        <MenuItem x:Name="check" Header="Check all" Click="CheckAll" />
                        <MenuItem x:Name="uncheck" Header="Uncheck all" Click="UnCheckAll" />
                    </ContextMenu>
                </ListBox.ContextMenu>

I have the following ContextMenu hooked on to a ListBox. I realy want to remove the space where the icon is. Is this possible in some easy way?

                    <ListBox.ContextMenu>
                    <ContextMenu >
                        <MenuItem x:Name="check" Header="Check all" Click="CheckAll" />
                        <MenuItem x:Name="uncheck" Header="Uncheck all" Click="UnCheckAll" />
                    </ContextMenu>
                </ListBox.ContextMenu>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-10-20 08:59:25

您需要更改 MenuItem 样式和控件模板。实际上,您需要为 MenuItem 的 4 个不同角色重新定义 4 个控件模板。这是您需要根据需要更改的起点代码。将其放置在您的窗口资源或其他适当的位置。

<!-- SimpleStyles: MenuItem -->

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
  <Setter Property="Height" Value="1"/>
  <Setter Property="Margin" Value="0,4,0,4"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Separator}">
        <Border BorderBrush="#888888" BorderThickness="1"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- TopLevelHeader -->

<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <ContentPresenter 
        Margin="6,3,6,3" 
        ContentSource="Header"
        RecognizesAccessKey="True" />
      <Popup 
        Name="Popup"
        Placement="Bottom"
        IsOpen="{TemplateBinding IsSubmenuOpen}"
        AllowsTransparency="True" 
        Focusable="False"
        PopupAnimation="Fade">
        <Border 
          Name="SubmenuBorder"
          SnapsToDevicePixels="True"
          Background="#FFFFFF"
          BorderBrush="#888888"
          BorderThickness="1" >
          <StackPanel  
            IsItemsHost="True" 
            KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsSuspendingPopupAnimation" Value="true">
      <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/>
      <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
    </Trigger>
    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
      <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/>
      <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- TopLevelItem -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <ContentPresenter 
        Margin="6,3,6,3" 
        ContentSource="Header"
        RecognizesAccessKey="True" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/>
      <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- SubmenuItem -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
        <ColumnDefinition Width="13"/>
      </Grid.ColumnDefinitions>
      <ContentPresenter 
        Name="Icon"
        Margin="6,0,6,0"
        VerticalAlignment="Center"
        ContentSource="Icon"/>
      <Border 
        Name="Check"  
        Width="13" Height="13" 
        Visibility="Collapsed"
        Margin="6,0,6,0" 
        Background="#C0C0C0"
        BorderThickness="1"
        BorderBrush="#404040">
        <Path 
          Name="CheckMark"
          Width="7" Height="7" 
          Visibility="Hidden" 
          SnapsToDevicePixels="False" 
          Stroke="#404040"
          StrokeThickness="2"
          Data="M 0 0 L 7 7 M 0 7 L 7 0" />
      </Border>
      <ContentPresenter 
        Name="HeaderHost"
        Grid.Column="1"
        ContentSource="Header"
        RecognizesAccessKey="True"/>
      <TextBlock x:Name="InputGestureText"
        Grid.Column="2"
        Text="{TemplateBinding InputGestureText}"
        Margin="5,2,0,2"
        DockPanel.Dock="Right" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="Icon" Value="{x:Null}">
      <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
    </Trigger>
    <Trigger Property="IsChecked" Value="true">
      <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
    </Trigger>
    <Trigger Property="IsCheckable" Value="true">
      <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
      <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- SubmenuHeader -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
        <ColumnDefinition Width="13"/>
      </Grid.ColumnDefinitions>
      <ContentPresenter 
        Name="Icon"
        Margin="6,0,6,0"
        VerticalAlignment="Center"
        ContentSource="Icon"/>
      <ContentPresenter 
        Name="HeaderHost"
        Grid.Column="1"
        ContentSource="Header"
        RecognizesAccessKey="True"/>
      <TextBlock x:Name="InputGestureText"
        Grid.Column="2"
        Text="{TemplateBinding InputGestureText}"
        Margin="5,2,2,2"
        DockPanel.Dock="Right"/>
      <Path 
        Grid.Column="3"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Data="M 0 0 L 0 7 L 4 3.5 Z" 
        Fill="#404040" />
      <Popup 
        Name="Popup"
        Placement="Right"
        HorizontalOffset="-4" 
        IsOpen="{TemplateBinding IsSubmenuOpen}"
        AllowsTransparency="True" 
        Focusable="False"
        PopupAnimation="Fade">
        <Border 
          Name="SubmenuBorder"
          SnapsToDevicePixels="True"
          Background="#FFFFFF"
          BorderBrush="#888888"
          BorderThickness="1" >
          <StackPanel  
            IsItemsHost="True" 
            KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="Icon" Value="{x:Null}">
      <Setter TargetName="Icon" Property="Visibility" Value="Collapsed"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
    </Trigger>
    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
      <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4"/>
      <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- MenuItem Style -->

<Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Style.Triggers>
    <Trigger Property="Role" Value="TopLevelHeader">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}"/>
      <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
    </Trigger>
    <Trigger Property="Role" Value="TopLevelItem">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}"/>
    </Trigger>
    <Trigger Property="Role" Value="SubmenuHeader">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}"/>
    </Trigger>
    <Trigger Property="Role" Value="SubmenuItem">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}"/>
    </Trigger>
  </Style.Triggers>
</Style>

You need to change MenuItem style and control template. Actually you need to redefine 4 control templates for 4 different roles of MenuItem. This is a starting point code you need to change for your needs. Place it in your window resources or other appropriate place.

<!-- SimpleStyles: MenuItem -->

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
  <Setter Property="Height" Value="1"/>
  <Setter Property="Margin" Value="0,4,0,4"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Separator}">
        <Border BorderBrush="#888888" BorderThickness="1"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- TopLevelHeader -->

<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <ContentPresenter 
        Margin="6,3,6,3" 
        ContentSource="Header"
        RecognizesAccessKey="True" />
      <Popup 
        Name="Popup"
        Placement="Bottom"
        IsOpen="{TemplateBinding IsSubmenuOpen}"
        AllowsTransparency="True" 
        Focusable="False"
        PopupAnimation="Fade">
        <Border 
          Name="SubmenuBorder"
          SnapsToDevicePixels="True"
          Background="#FFFFFF"
          BorderBrush="#888888"
          BorderThickness="1" >
          <StackPanel  
            IsItemsHost="True" 
            KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsSuspendingPopupAnimation" Value="true">
      <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/>
      <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
    </Trigger>
    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
      <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/>
      <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- TopLevelItem -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <ContentPresenter 
        Margin="6,3,6,3" 
        ContentSource="Header"
        RecognizesAccessKey="True" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/>
      <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- SubmenuItem -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
        <ColumnDefinition Width="13"/>
      </Grid.ColumnDefinitions>
      <ContentPresenter 
        Name="Icon"
        Margin="6,0,6,0"
        VerticalAlignment="Center"
        ContentSource="Icon"/>
      <Border 
        Name="Check"  
        Width="13" Height="13" 
        Visibility="Collapsed"
        Margin="6,0,6,0" 
        Background="#C0C0C0"
        BorderThickness="1"
        BorderBrush="#404040">
        <Path 
          Name="CheckMark"
          Width="7" Height="7" 
          Visibility="Hidden" 
          SnapsToDevicePixels="False" 
          Stroke="#404040"
          StrokeThickness="2"
          Data="M 0 0 L 7 7 M 0 7 L 7 0" />
      </Border>
      <ContentPresenter 
        Name="HeaderHost"
        Grid.Column="1"
        ContentSource="Header"
        RecognizesAccessKey="True"/>
      <TextBlock x:Name="InputGestureText"
        Grid.Column="2"
        Text="{TemplateBinding InputGestureText}"
        Margin="5,2,0,2"
        DockPanel.Dock="Right" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="Icon" Value="{x:Null}">
      <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
    </Trigger>
    <Trigger Property="IsChecked" Value="true">
      <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
    </Trigger>
    <Trigger Property="IsCheckable" Value="true">
      <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
      <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- SubmenuHeader -->

<ControlTemplate 
  x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" 
  TargetType="{x:Type MenuItem}">
  <Border Name="Border" >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
        <ColumnDefinition Width="13"/>
      </Grid.ColumnDefinitions>
      <ContentPresenter 
        Name="Icon"
        Margin="6,0,6,0"
        VerticalAlignment="Center"
        ContentSource="Icon"/>
      <ContentPresenter 
        Name="HeaderHost"
        Grid.Column="1"
        ContentSource="Header"
        RecognizesAccessKey="True"/>
      <TextBlock x:Name="InputGestureText"
        Grid.Column="2"
        Text="{TemplateBinding InputGestureText}"
        Margin="5,2,2,2"
        DockPanel.Dock="Right"/>
      <Path 
        Grid.Column="3"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Data="M 0 0 L 0 7 L 4 3.5 Z" 
        Fill="#404040" />
      <Popup 
        Name="Popup"
        Placement="Right"
        HorizontalOffset="-4" 
        IsOpen="{TemplateBinding IsSubmenuOpen}"
        AllowsTransparency="True" 
        Focusable="False"
        PopupAnimation="Fade">
        <Border 
          Name="SubmenuBorder"
          SnapsToDevicePixels="True"
          Background="#FFFFFF"
          BorderBrush="#888888"
          BorderThickness="1" >
          <StackPanel  
            IsItemsHost="True" 
            KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="Icon" Value="{x:Null}">
      <Setter TargetName="Icon" Property="Visibility" Value="Collapsed"/>
    </Trigger>
    <Trigger Property="IsHighlighted" Value="true">
      <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
    </Trigger>
    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
      <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4"/>
      <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Foreground" Value="#888888"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- MenuItem Style -->

<Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Style.Triggers>
    <Trigger Property="Role" Value="TopLevelHeader">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}"/>
      <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
    </Trigger>
    <Trigger Property="Role" Value="TopLevelItem">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}"/>
    </Trigger>
    <Trigger Property="Role" Value="SubmenuHeader">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}"/>
    </Trigger>
    <Trigger Property="Role" Value="SubmenuItem">
      <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}"/>
    </Trigger>
  </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文