2012-05-01 15 views
2

私はwpfでグリッドを持っています。矩形でマウスオーバーすると色の変化が見えます。しかし、コンテンツにマウスオーバーすると、元の矩形の色が見えます。wpf mouseover fill rectangle

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ハンドラを設定すると、うまく動作するはずです。