c# - WPF ItemsControl IsMouseOver 未按预期工作

标签 c# wpf silverlight xaml

我在 WPF 窗口的 Window.Resources 中有以下代码。它基本上是在创建一个项目,该项目表示一个网格,左侧有一个标签,右侧有一个按钮。当我将鼠标悬停在标签或按钮上时,该行会按预期改变颜色,但如果鼠标悬停在任何一行上,我希望它也改变颜色。

如何实现?

感谢任何帮助。

<Window.Resources>
    <dtos:ProjectDto x:Key="data"/>
    <Style x:Key="alternatingWithTriggers" 
           TargetType="{x:Type ContentPresenter}">
        <Setter Property="Height" Value="25"></Setter>
    </Style>
    <Style x:Key="onmouseover" TargetType="{x:Type DockPanel}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow">
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="ItemTemplate">
        <Border x:Name="ItemBorder" HorizontalAlignment="Stretch" BorderThickness="0" Background="#BBB" ClipToBounds="True" >
            <DockPanel ClipToBounds="True" HorizontalAlignment="Stretch" Style="{StaticResource onmouseover}">
                <Label Content="{Binding Name}" HorizontalAlignment="Left" Height="80"></Label>
                <Button Content="Delete" HorizontalAlignment="Right" Margin="0,0,10,0"/>
            </DockPanel>
        </Border>
...

最佳答案

给 DockPanel Background="Transparent"。这应该允许它捕获鼠标事件。

关于c# - WPF ItemsControl IsMouseOver 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1985987/

相关文章:

c# - 如何使用 MVVM 在 WPF 的主窗口中显示用户控件

visual-studio-2008 - 在安装vs2010\silverlight 4之后在vs2008中调试Silverlight 3

c# - Intellisense 不适用于 Visual Studio Professional 2017 中的单元测试项目

c# - Json.Net 仅在 Visual Studio 中抛出 OutOfMemoryException

wpf - 为堆栈面板内的控件赋予相等的宽度

silverlight - Silverlight 4提供哪些线程安全的收集类?

silverlight - DataGridTemplateColumn 内的绑定(bind)命令

c# - 如何让 C# 中的 notifyicon 在两个图标之间交替?

c# - 在一个 LINQ 语句中选择值并向其附加数字?

c# - 适合 C# 开发人员用于视觉上令人惊叹的表示层的良好框架