0

I have to stop the mouse over event bubbling for following scenario.

  <Window.Resources>
    <x:Array x:Key="strings" Type="sys:String" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:String>One</sys:String>
        <sys:String>Two</sys:String>
    </x:Array>
    <DataTemplate x:Key="MyDataTemplate">
        <TextBlock Text="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"/>
    </DataTemplate>

    <DataTemplate x:Key="MyParentDataTemplate">
        <Expander HeaderTemplate="{StaticResource MyDataTemplate}">
            <ListView
              ItemsSource="{StaticResource strings}"
              ItemTemplate="{StaticResource MyDataTemplate}">
            </ListView>
        </Expander>
    </DataTemplate>

    <Style x:Key="ParentListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Foreground" Value="Blue"/>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListView ItemContainerStyle="{StaticResource ParentListViewItemStyle}"
              ItemsSource="{StaticResource strings}"
              ItemTemplate="{StaticResource MyParentDataTemplate}">
    </ListView>
</Grid>

The problem is when I do the mouse over on any child listviewitem. The mouse over of respective parent also get true. But I just dont want the parent to get mouseover.

I tried to attach the behavior and tried to set the e.Handled = true but this did not worked.

D J
  • 6,908
  • 13
  • 43
  • 75
  • why do you have to do this? – markmnl Jan 07 '14 at 02:44
  • I have some common datatemplate which I am using in both. I want to give different effects in parent and child on mouse over but I cant. – D J Jan 07 '14 at 03:24
  • Write a custom control that inherit from ListView. In my opinion this is the way to go for such a specific requirements. – jwillmer Jan 07 '14 at 06:40

1 Answers1

0

IsMouseOver is not tied to whether the MouseOver event fired for an element. It is tied to whether the element is the Mouse.DirectlyOver or one of its ancestors. The ancestor(s) of the element under the mouse will always have their IsMouseOver set to true. I don't know what you are specifically trying to accomplish but if IsMouseOver doesn't suit your needs then you'll likely need to create and manage your own dependency property based on whatever your requirements are.

AndrewS
  • 6,054
  • 24
  • 31