In my application, I have a TreeView that allows drag/drop. I have all the functionality working fine, however I am having difficulty highlighting a TreeViewItem when it is dragged over. Here is my style for my treeview item. The IsMouseOver trigger does not work while dragging, because dragging seems to block other mouse events. Can anyone help me trigger the same border changes on my treeview item while dragging?
<Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
<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="PART_Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"
/>
<Border
x:Name="OuterBorder"
Grid.Column="1"
SnapsToDevicePixels="True"
BorderThickness="1"
CornerRadius="3"
BorderBrush="Transparent"
Background="Transparent"
>
<Border
x:Name="InnerBorder"
SnapsToDevicePixels="True"
BorderThickness="1"
CornerRadius="2"
BorderBrush="Transparent"
Background="Transparent"
>
<ContentPresenter
x:Name="PART_Content"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
/>
</Border>
</Border>
<ItemsPresenter
x:Name="PART_ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" SourceName="OuterBorder" Value="True">
<Setter TargetName="OuterBorder" Property="BorderBrush" Value="Blue" />
<Setter TargetName="OuterBorder" Property="Background" Value="Red" />
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="White" />
</Trigger>
<MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In addition to Stefan's answer, I found that is necessary to add also the Drop event:
and registering drop event also:
Otherwise if drop occurs of one treeItem over another, the background might not change. Same thing if you press ESC.
I'm using an attached property for this, and then use that property in my xaml file to change the background color of the tree view item:
and then in the xaml file:
This post is simply a porting of Stefan's awesome response above to VB, for those of us working in those confines; I have nothing to offer except to hope I didn't screw anything up too badly. It appears to work for me:
End Namespace
Look at the DragOver event (and possibly DragEnter/DragLeave) instead of IsMouseOver.