2

我在 wpf 中有网格。当我在矩形上做鼠标悬停时,我可以看到颜色变化。但是当我在内容上做鼠标悬停时,我看到了矩形的原始颜色。

我应该写什么来在 ContentPresenter 上应用相同的鼠标悬停效果,或者有没有办法在内容演示者的鼠标悬停时更改矩形背景颜色。

<Grid Background="{TemplateBinding Background}" x:Name="dgColumnHeader">
         <Border x:Name="border" BorderBrush="Black" BorderThickness="0,0,1,1" Grid.ColumnSpan="1">
                <Rectangle Width="116" Margin="3,3,3,3" HorizontalAlignment="Center" RadiusX="7" RadiusY="7">
                    <Rectangle.Style>
                        <Style TargetType="{x:Type Rectangle}">
                            <Setter Property="Fill" Value="{DynamicResource ContentOutofFocusBrush}"></Setter>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Fill" Value="{DynamicResource ActiveItemBrush}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Rectangle.Style>
                </Rectangle>
            </Border>
            <ContentPresenter x:Name="content"  HorizontalAlignment="Center"  VerticalAlignment="Center" Content="{TemplateBinding Content}" />
        </Grid>

谢谢迪

4

2 回答 2

3

如果网格是您的控件模板的一部分,那么最好将矩形样式触发器移动到 ControlTemplate.Triggers 中:

<Window x:Class="Presentation2.MouseOverRectangleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseOverRectangleWindow" Height="300" Width="300">
  <Window.Resources>
    <SolidColorBrush x:Key="ContentOutofFocusBrush" Color="Orange"/>

    <SolidColorBrush x:Key="ActiveItemBrush" Color="Blue" />

    <Style x:Key="MouseOverContentControlStyle" TargetType="{x:Type ContentControl}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="ContentControl">
            <Grid Background="{TemplateBinding Background}" x:Name="dgColumnHeader">
              <Border x:Name="border" BorderBrush="Black" BorderThickness="0,0,1,1" Grid.ColumnSpan="1">
                <Rectangle x:Name="PART_Rectangle" Width="116" Margin="3,3,3,3" HorizontalAlignment="Center" RadiusX="7" RadiusY="7">
                  <Rectangle.Style>
                    <Style TargetType="{x:Type Rectangle}">
                      <Setter Property="Fill" Value="{DynamicResource ContentOutofFocusBrush}"></Setter>
                    </Style>
                  </Rectangle.Style>
               </Rectangle>
              </Border>
             <ContentPresenter x:Name="content" HorizontalAlignment="Center"  VerticalAlignment="Center" Content="{TemplateBinding Content}" />
           </Grid>
           <ControlTemplate.Triggers>
             <Trigger Property="IsMouseOver" Value="True">
               <Setter TargetName="PART_Rectangle" Property="Fill" Value="{DynamicResource ActiveItemBrush}" />
             </Trigger>
           </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
  </Setter>
</Style>
</Window.Resources>
   <Grid>
     <ContentControl Style="{StaticResource MouseOverContentControlStyle}">
       <TextBlock Text="Hello World!" />
     </ContentControl>
   </Grid>
</Window>
于 2012-05-01T06:07:17.350 回答
2

你不需要在你的边框内有一个矩形。改变边框的背景,你会得到相同的结果。然后将 ContentPresenter 放在该边框内,并在边框上设置 MouseOver 处理程序,它应该可以正常工作。

于 2012-05-01T09:14:32.957 回答