2012-05-01 147 views
2

我有網的WPF。當我在矩形上進行鼠標懸停時,我可以看到顏色變化。但是當我在內容上進行鼠標懸停時,我會看到矩形的原始顏色。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> 

感謝 迪

回答

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

這是真的真棒,它爲我工作很多試驗和錯誤之後。在網格內有矩形時設置控件模板。 –

2

你不需要邊框內的矩形。更改邊框的背景,你會得到相同的結果。然後將ContentPresenter放在該邊框內,並在邊框上設置MouseOver處理程序,它應該可以正常工作。