我在 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/