wpf - 修改 ItemsControl 中项目的 Z 索引

标签 wpf z-index itemscontrol rendertransform

这是我的 ItemsControl 的代码,它在鼠标移过时放大项目。
我无法增加当前缩放项目的 ZIndex 以将其置于其他项目之上。

<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>

我试图直接更改触发器中的 ZIndex,但它不起作用。
似乎我需要更改 ContentPresenter 中的 ZIndex,它是 VisualTree 中 TextBlock 的父级,而不是直接在 TextBlock 中。
<Setter Property="Panel.ZIndex" Value="99" />

所以我试图在 ContentPresenter 中更改 ZIndex,但它仍然不起作用
<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>

有谁知道它是如何工作的?

最佳答案

我只是完全按照您在 WPF 4 中的建议进行了尝试,并且效果很好。

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"
            };
        }
    }
}

是什么让你认为它不起作用?你用过Snoop验证过吗?

关于wpf - 修改 ItemsControl 中项目的 Z 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9687674/

相关文章:

c# - Telerik RadGauge + 数据绑定(bind)

html - z-index 和 iFrame!

WPF 数据绑定(bind)项目符号列表

c# - 从 ItemsControl 中删除项目

c# - 在 WPF 中使用 ViewModel 创建 UserControl

c# - 如何在 DatePicker 上使用交互性 :Interaction. 触发器

html - z-index 不在父项后面显示子项

html - Z-Index 堆叠?

WPF ItemsControl - ViewModel 上的命令未从 ItemsControl 中触发

c# - 如何将带有 contextMenu 的 WPF ListView 绑定(bind)到 viewModel