【问题标题】:WPF Remove mouseover / hover border on Menuitems and ListViewWPF 删除 Menuitems 和 ListView 上的鼠标悬停/悬停边框
【发布时间】:2016-11-11 22:28:48
【问题描述】:

我已经到处寻找这个问题的答案,但还没有找到实现我需要做的事情的方法,所以我想我会问我自己的问题。

我的 UserControl 和 ListView 中有 2 个菜单。每当鼠标悬停在任何菜单项(在本例中为下图中的“结束日”按钮)或列表视图上时,控件周围会出现一个细边框,然后当鼠标移到其他地方时消失。我不知道这叫什么或如何找到它(在 XAML 或 Blend 中),但我很想学习如何为 menuitems 和 listview (以及其他控件以及如果有通用的方法)禁用它这样做),最好在 XAML 中。我觉得这很烦人。如果可以的话,请在这里帮助我。

更新:我的 ListView:

<ListView ItemsSource="{Binding Adventurers}"
              Name="AdvListView"
              ScrollViewer.CanContentScroll="False"
              Background="Gray"
              BorderBrush="Transparent"
              Grid.Column="1"
              Grid.ColumnSpan="3"
              Grid.Row="2">

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=ShowAdvCommand}"
                                    CommandParameter="{Binding ElementName=AdvListView, Path=SelectedItem}"
                                    PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto" Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Width="Auto" Header="Level" DisplayMemberBinding="{Binding Level}"/>
            </GridView>
        </ListView.View>
    </ListView>

我的菜单:

<Menu Background="#FFA9D1F4"
          Grid.ColumnSpan="5" 
          IsMainMenu="False">
        <Menu.ItemsPanel>
            <ItemsPanelTemplate>
                <DockPanel HorizontalAlignment="Stretch" />
            </ItemsPanelTemplate>
        </Menu.ItemsPanel>
        <MenuItem Header="File"
                  Focusable="False"
                  FontFamily="Pericles"
                  FontSize="16"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Left">


            <MenuItem Header="Save" 
                      Command="{Binding SaveGame}" />

            <MenuItem Header="Load" 
                      Command="{Binding LoadGame}" />

            <MenuItem Header="Quit" />
        </MenuItem>

        <Button Content="End Day" 
                Command="{Binding EndDayCommand}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

        <Button Content="Load" 
                Command="{Binding LoadGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

        <Button Content="Save" 
                Command="{Binding SaveGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

        <Label Content="{Binding GameDate}"
               Focusable="False"
               ContentStringFormat="{}{0:d\/M\/y}"
               FontFamily="Pericles"
               FontSize="16"
               VerticalAlignment="Center"
               HorizontalAlignment="Right" />
    </Menu>

【问题讨论】:

  • 您能提供一些您的 xaml 的代码 sn-ps 吗?重点关注控件声明及其样式(显式或隐式设置)
  • 我已经更新了我的帖子。
  • 很好,没有样式?你确定吗?根据图片,其中涉及样式,请检查您的 xaml 中的父节点是否有包含样式的资源节点或使用的 usercontrol 资源字典。
  • 我的 XAML 中没有任何涉及样式的内容。
  • 我已经调查过了,这个更改涉及修改默认模板,我会为你发布一个答案,但我必须警告你这将是一个很长的代码。

标签: wpf xaml


【解决方案1】:

问题是您在菜单定义中拥有按钮,这不是菜单的常见用途,因为它应该包含菜单项。我相信您需要一个包含一些按钮和一个菜单的“菜单”,我为您提供了一个简单的解决方案,您应该针对您的特定代码进行调整:

另一个不推荐的解决方案是潜入 menuItem 和菜单默认控件模板并“从头开始”编写它们,因为您不能在部分情况下覆盖默认样式,这里有一个示例说明如何做到这一点(再次 - 在这种情况下强烈不推荐):

http://msdn.microsoft.com/en-us/library/ms747082(v=vs.85).aspx

<Window x:Class="WpfApplication4.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">
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <Menu Background="#FFA9D1F4"

              BorderBrush="{x:Null}"
          IsMainMenu="False">
                <Menu.ItemsPanel>
                    <ItemsPanelTemplate>
                        <DockPanel HorizontalAlignment="Stretch" />
                    </ItemsPanelTemplate>
                </Menu.ItemsPanel>
                <MenuItem Header="File"
                  Focusable="False"
                  FontFamily="Pericles"
                  FontSize="16"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Left">


                    <MenuItem Header="Save" 
                      Command="{Binding SaveGame}" />

                    <MenuItem Header="Load" 
                      Command="{Binding LoadGame}" />

                    <MenuItem Header="Quit" />
                </MenuItem>
            </Menu>

            <Button Content="End Day" 
                Command="{Binding EndDayCommand}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />


                <Label Content="GameDate"
               Focusable="False"
               ContentStringFormat="{}{0:d\/M\/y}"
               FontFamily="Pericles"
               FontSize="16"
               VerticalAlignment="Center"
               HorizontalAlignment="Right" />
            <Button Content="Load" 
                Command="{Binding LoadGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

                <Button Content="Save" 
                Command="{Binding SaveGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />
        </StackPanel>

    </Grid>
</Window>

编辑:

我做了一些重构,使它看起来像一个完整解决方案的“好看”菜单栏,请尝试一下。

对于您的列表视图,我相信问题以非常相似的方式解决。

    <Window x:Class="WpfApplication4.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">
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Margin" Value="5"></Setter>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <StackPanel Background="#FFA9D1F4" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
            <Menu 
         Background="#FFA9D1F4"
              BorderBrush="{x:Null}"
          IsMainMenu="False">
                <Menu.ItemsPanel>
                    <ItemsPanelTemplate>
                        <DockPanel HorizontalAlignment="Stretch" />
                    </ItemsPanelTemplate>
                </Menu.ItemsPanel>
                <Menu.Resources>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Background" Value="#FFA9D1F4"></Setter>
                    </Style>
                </Menu.Resources>
                <MenuItem Header="File"
                  Focusable="False"
                  FontFamily="Pericles"
                  FontSize="16"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Left">


                    <MenuItem Header="Save" 
                              Background="#FFA9D1F4"
                      Command="{Binding SaveGame}" />

                    <MenuItem Header="Load" 
                              Background="#FFA9D1F4"
                      Command="{Binding LoadGame}" />

                    <MenuItem Header="Quit" />
                </MenuItem>
            </Menu>

            <Button Content="End Day" 
                Command="{Binding EndDayCommand}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />



            <Button Content="Load" 
                Command="{Binding LoadGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

                <Button Content="Save" 
                Command="{Binding SaveGame}" 
                Focusable="False"
                FontFamily="Pericles"
                FontSize="16"
                VerticalAlignment="Center"
                HorizontalAlignment="Left" />

        </StackPanel>
        <Label Margin="0" Grid.Row="0" Grid.Column="1" Content="1/1/13"
               Focusable="False"
               ContentStringFormat="{}{0:d\/M\/y}"
               FontFamily="Pericles"
               FontSize="16"
               VerticalAlignment="Center"
               HorizontalAlignment="Right" />

    </Grid>
</Window>

【讨论】:

    【解决方案2】:

    在混合中,您可以右键单击按钮,然后单击“编辑模板副本”或类似的内容(不是逐字记录)。

    它将打开按钮的实际模板,在那里您可以编辑视觉状态。在这里您可以完全删除或编辑悬停效果。

    对不起,我不能给你更多的细节。

    【讨论】:

      【解决方案3】:

      对我有用的是像这样使用 IsHitTestVisible-tag:

      <Menu IsHitTestVisible="False" > ... </Menu>
      

      你也可以尝试结合Focusable="False"

      基本上我用它来完全禁止用户点击菜单(这正是我想要的用例)。

      【讨论】:

        猜你喜欢
        • 2013-06-15
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 2011-05-26
        相关资源
        最近更新 更多