Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Triggers on TreeViewItems and escaping the routing

Closed Thread
 
Thread Tools Display Modes
Old 09-11-2006   #1 (permalink)
Martin Smith
Guest


 

Triggers on TreeViewItems and escaping the routing

I wanted to give a highlight to a TreeViewItem in a tree like this:

<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="{x:Static Cursors.Hand}"
/>
<Setter Property="Background" Value="LightGray" />
</Trigger>
</Style.Triggers>
</Style>

But when I do this the TreeViewItem parent gets highlighted along with
the Item i've moused over. I tried IsMouseDirectlyOver, but that
didn't work as I'm sure the mouse was directly over a TextBlock or
something. Is there any way to use a trigger (eg. no c# code) and get
an individual TreeViewItem selected on MouseOver?

Thanks,
Martin

Old 09-15-2006   #2 (permalink)
=?Utf-8?B?WXV2YWw=?=
Guest


 

RE: Triggers on TreeViewItems and escaping the routing

Hi Martin,

I struggled with this one as well. I came up with the solution, but it is a
long one since it is based on overriding the TreeViewItem control template in
order to achieve the proper mouseover effect. Notice, in particular, how the
"IsMouseOver" trigger is qualified with the SourceName property:


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Paper3D"
Height="720" Width="800">
<Page.Resources>

<Style x:Key="Äž"
TargetType="{x:Type ToggleButton}">
<Setter Property="Focusable"
Value="False"/>
<Setter Property="Width"
Value="19"/>
<Setter Property="Height"
Value="13"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Width="19"
Height="13"
Background="Transparent">
<Border Width="9"
Height="9"
BorderThickness="1"
BorderBrush="{DynamicResource {x:Static
SystemColors.GrayTextBrushKey}}"
Background="{DynamicResource {x:Static
SystemColors.WindowBrushKey}}"
SnapsToDevicePixels="true">
<Path x:Name="ExpandPath"
Margin="1,1,1,1"
Fill="{DynamicResource {x:Static
SystemColors.WindowTextBrushKey}}"
Data="M 0 2 L 0 3 L 2 3 L 2 5 L 3 5 L 3 3
L 5 3 L 5 2 L 3 2 L 3 0 L 2 0 L 2 2 Z"/>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="True">
<Setter Property="Data"
TargetName="ExpandPath"
Value="M 0 2 L 0 3 L 5 3 L 5 2 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="{x:Type TreeViewItem}"
TargetType="{x:Type TreeViewItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="1,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19"
Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander"
Style="{StaticResource Äž}"
IsChecked="{Binding
Path=IsExpanded,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<Border Name="Bd"
Grid.Column="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding
BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ContentPresenter x:Name="PART_Header"
ContentSource="Header"

HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_Header"
Property="IsMouseOver"
Value="True">
<Setter TargetName="Bd"
Property="Background"
Value="Red"/>
</Trigger>
<Trigger Property="IsExpanded"
Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
<Trigger Property="HasItems"
Value="false">
<Setter TargetName="Expander"
Property="Visibility"
Value="Hidden"/>
</Trigger>
<Trigger Property="IsSelected"
Value="true">
<Setter TargetName="Bd"
Property="Background"
Value="{DynamicResource {x:Static
SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static
SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected"
Value="true"/>
<Condition Property="IsSelectionActive"
Value="false"/>
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="{DynamicResource {x:Static
SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static
SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static
SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>



</Page.Resources>

<StackPanel>
<TreeView Width="200" Height="200">
<TreeViewItem Header="Node 1"/>
<TreeViewItem Header="Node 2">
<TreeViewItem Header="Node 2.1"/>
<TreeViewItem Header="Node 2.2">
<TreeViewItem Header="Node 2.2.1"/>
<TreeViewItem Header="Node 2.2.2"/>
</TreeViewItem>
<TreeViewItem Header="Node 2.3"/>
</TreeViewItem>
<TreeViewItem Header="Node 3"/>
</TreeView>
</StackPanel>
</Page>


Cheers,
Yuval




"Martin Smith" wrote:

> I wanted to give a highlight to a TreeViewItem in a tree like this:
>
> <Style TargetType="{x:Type TreeViewItem}">
> <Setter Property="IsExpanded" Value="True" />
> <Style.Triggers>
> <Trigger Property="IsMouseOver" Value="True">
> <Setter Property="Cursor" Value="{x:Static Cursors.Hand}"
> />
> <Setter Property="Background" Value="LightGray" />
> </Trigger>
> </Style.Triggers>
> </Style>
>
> But when I do this the TreeViewItem parent gets highlighted along with
> the Item i've moused over. I tried IsMouseDirectlyOver, but that
> didn't work as I'm sure the mouse was directly over a TextBlock or
> something. Is there any way to use a trigger (eg. no c# code) and get
> an individual TreeViewItem selected on MouseOver?
>
> Thanks,
> Martin
>
>

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
autorun triggers Scan & Fix beano Vista General 0 05-02-2008 01:54 PM
Ext HD triggers new device, Won't install GettingOld Vista hardware & devices 8 04-04-2008 06:08 PM
Passing parameters to PS script and escaping spaces James PowerShell 2 08-14-2007 07:30 AM
Escaping problem. Kevin Burton PowerShell 1 11-22-2006 11:21 PM
StackPanel.Triggers issue Vipin [MVP] Avalon 2 06-01-2006 08:55 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50