【问题标题】:wpf mouseover fill rectanglewpf 鼠标悬停填充矩形
【发布时间】:2012-05-10 16:41:26
【问题描述】:

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

我应该写什么来在 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>

谢谢 深

【问题讨论】:

    标签: wpf mouseover


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果网格是控件模板的一部分,那么最好将矩形样式触发器移动到 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>
      

      【讨论】:

      • 这真是太棒了,经过多次试验和错误,它对我有用。当网格中有矩形时设置控件模板。
      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2018-10-15
      • 2012-12-29
      • 2011-11-09
      • 2012-03-01
      相关资源
      最近更新 更多