WPF删除Menuitems和ListView上的鼠标悬停/悬停边框

时间:2013-06-04 19:12:40

标签: wpf xaml

我已经全神贯过地寻找答案,并没有找到实现我需要做的事情的方法,所以我想我会问自己的问题。

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

enter image description here

更新:我的列表视图:

<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>

3 个答案:

答案 0 :(得分: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>

修改

我做了一些重构,所以它看起来像一个'漂亮的'菜单栏,提供完整的解决方案,请尝试一下。

列表视图我认为问题以非常类似的方式得到解决。

Menu outcome

    <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>

答案 1 :(得分:0)

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

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

抱歉,我无法向您提供更多详细信息。

答案 2 :(得分:0)

对我有用的是使用像这样的IsHitTestVisible标签:

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

您也可以与Focusable="False"

结合使用

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