views:

95

answers:

2

I want to change the background colour of the rectangle in the IsMouseOver trigger, is this possible?

<Window>
    <Window.Resources>
     <DataTemplate x:Key="StackListViewItemTemplate">
       <Grid>
        <Rectangle RadiusX="5" RadiusY="5" >
         <Rectangle.Fill>
          <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
           <GradientStop Color="#FF000000" Offset="0"/>
           <GradientStop Color="{Binding Path=Events.Colour}" Offset="1"/>
          </LinearGradientBrush>
         </Rectangle.Fill>
        </Rectangle>
        <TextBlock FontSize="18pt" Grid.RowSpan="2" Text="{Binding Path=Events.Name}" HorizontalAlignment="Center" VerticalAlignment="Center"  />
       </Grid>
      </Grid>
     </DataTemplate>
    </Window.Resources>

    <Grid >
     <ListView ItemTemplate="{DynamicResource StackListViewItemTemplate}">
      <ListView.ItemContainerStyle>
       <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
         <Trigger Property="IsMouseOver" Value="true">
          <!-- Here I want to set the background colour of the Rectangle in the DataTemplate -->
         </Trigger>
         <Trigger Property="IsSelected" Value="true">
          <!-- Same here -->
         </Trigger>
        </Style.Triggers>

       </Style>
      </ListView.ItemContainerStyle>
     </ListView>
    </Grid>

</Window>

Edit:

<Setter Property="Background" Value="Yellow"/>

Is there a way to bind the Property value of the setter to child controls?

A: 

Yes, you should be able to do something like this:

XAML Tutorial - Changing the Text Color on Mouse Over

...but with a rectangle of course.

Justin Niessner
The problem is with the ListView and the DataTemplate. The trigger works, but it sets the background of the ListViewItem which wraps the Grid and Rectangle. I want to change the bacground of just the rectangle.
monkey_p
A: 

I got it working using a datatrigger in the template

<DataTemplate x:Key="listItemTemplate" DataType="ListViewItem">
    <Grid>
     <Rectangle Name="myRectangle" RadiusX="5" RadiusY="5" >
      <Rectangle.Fill>
       <SolidColorBrush Color="Red" />
      </Rectangle.Fill>
     </Rectangle>
    </Grid>
    <DataTemplate.Triggers>
     <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="True">
      <Setter TargetName="myRectangle" Property="Fill" >
       <Setter.Value>
        <SolidColorBrush Color="Green" />
       </Setter.Value>
      </Setter>
     </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
monkey_p