Change ZIndex items in ItemsControl

Here is the code for my ItemsControl, which scales objects on mouse click.
I am unable to increase the ZIndex of the current enlarged item to shift it to the rest.

<ItemsControl ItemsSource="{Binding Path=Value}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Name}" RenderTransformOrigin="0.5 0.5"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="RenderTransform"> <Setter.Value> <ScaleTransform ScaleX="1.5" ScaleY="1.5" /> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> 

I tried to change ZIndex directly in the trigger, but it does not work.
It seems I need to change ZIndex in ContentPresenter, which is the parent of TextBlock in VisualTree, and not directly in TextBlock.

 <Setter Property="Panel.ZIndex" Value="99" /> 

So, I tried changing ZIndex in ContentPresenter, but it still does not work.

 <ItemsControl.ItemContainerStyle> <Style TargetType="{x:Type ContentPresenter}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Panel.ZIndex" Value="99" /> </Trigger> </Style.Triggers> </Style> </ItemsControl.ItemContainerStyle> 

Does anyone know how this works?

+6
source share
1 answer

I just tried exactly what you suggested in WPF 4 and it worked fine.

MainWindow.xaml

 <Window x:Class="SO9687674.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <ItemsControl ItemsSource="{Binding}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="RenderTransform"> <Setter.Value> <ScaleTransform ScaleX="2.5" ScaleY="2.5" /> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemContainerStyle> <Style TargetType="{x:Type ContentPresenter}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Panel.ZIndex" Value="99" /> </Trigger> </Style.Triggers> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </Window> 

MainWindow.xaml.cs

 using System.Collections.Generic; using System.Windows; namespace SO9687674 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new List<string> { "One", "two", "three" }; } } } 

Why do you think that it does not work? Have you used Snoop to test?

+10
source

Source: https://habr.com/ru/post/910609/


All Articles