【问题标题】:Modify ZIndex of an Items in an ItemsControl修改 ItemsControl 中项目的 ZIndex
【发布时间】:2012-03-13 16:05:20
【问题描述】:

这是我的 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>

有人知道它是如何工作的吗?

【问题讨论】:

  • 使用 Canvas 对我来说很好。所以你可能还有另一个问题。您使用哪种面板?
  • 我使用 WrapPanel。哪种解决方案适合您?

标签: wpf z-index itemscontrol rendertransform


【解决方案1】:

我刚刚完全按照您在 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验证了吗?

【讨论】:

  • 我认为它不起作用,因为我看到它不起作用:) 我窥探它并且当我的鼠标继续时 ZIndex 不会改变。我将在一个新项目中尝试您的示例。
  • @Nicolas:冒着光顾的风险,你在窥探容器,而不是TextBlock 本身,对吧?
  • @Nicolas 唯一可能行不通的方法是,如果您有其他东西编写 Zindex 属性。检查value precedence,如果具有更高优先级的东西正在写入zindex,则触发值被“忽略”,并没有真正被忽略,但只要更高优先级写入值就不会使用。
  • @Kent:谢谢你的例子,我设法让它工作。你让我回到正确的道路上。我查找了问题,发现我有一个 ItemsControl 的串联,我把 ItemContainerStyle 放在了错误的 :( 感谢您的有用帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多