从DataTemplate绑定ZIndex [英] Binding ZIndex from DataTemplate

查看:88
本文介绍了从DataTemplate绑定ZIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本设置如下的视图:

I have a View that is basically setup like this:

<Grid>
<ViewBox>
    <Grid>
        <ItemsControl ItemsSource="{Binding MyItems}"
                      ItemTemplate="{Binding Source={StaticResource MyItemsDataTemplate}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</ViewBox>
</Grid>

此处使用的DataTemplate可以简化为:

The DataTemplate used here can be reduced to this:

<DataTemplate x:Key="AreaItemDisplayDataTemplate">
<Canvas Grid.ZIndex={Binding Z}>
    <Grid>
        // an shape is displayed here...
    </Grid>
</Canvas>

我现在期望ZIndex绑定到单个项目的Z属性。当我调试代码时,我还可以看到,Z属性获取器在我期望的时候可以访问(例如,每当我引发它的propertychanged事件时),因此我认为绑定可以正常工作。

I would now expect the ZIndex to be bound to the Z property of the individual items. When i debug the code, i can also see, that the Z property getter is accessed when i would expect it (whenever i raise the propertychanged event for it, eg) so i assume the binding to work correctly.

但是,ZIndex无法正常工作。绑定到值对实际显示的Z顺序不起作用。该代码在哪里出问题?

However, the ZIndex is not working as expected. The binding to the value has no effect on the actual displayed Z Order. Where am i going wrong with this code?

推荐答案

DataTemplate 被包裹在 ContentPresenter 中,因此 DataTemplate Canvas c>不是 ItemsPanel Grid 的直接子代。这就是 ZIndex 属性不执行任何操作的原因。

The content of the DataTemplate gets wrapped in a ContentPresenter so the Canvas in the DataTemplate isn't a direct child of the ItemsPanel Grid. That is the reason the ZIndex property doesn't do anything.

移动 ZIndex 绑定 ItemContainerStyle ,它应该可以工作。

Move the ZIndex Binding to the ItemContainerStyle and it should work.

<ItemsControl ItemsSource="{Binding MyItems}" 
              ItemTemplate="{Binding Source={StaticResource MyItemsDataTemplate}}"> 
    <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
            <Grid /> 
        </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.ZIndex" Value="{Binding Z}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl> 

这篇关于从DataTemplate绑定ZIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆