从DataTemplate绑定ZIndex

Urb*_*Esc 3 c# wpf xaml binding datatemplate

我有一个基本上像这样设置的视图:

<Grid>
<ViewBox>
    <Grid>
        <ItemsControl ItemsSource="{Binding MyItems}"
                      ItemTemplate="{Binding Source={StaticResource MyItemsDataTemplate}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</ViewBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这里使用的DataTemplate可以简化为:

<DataTemplate x:Key="AreaItemDisplayDataTemplate">
<Canvas Grid.ZIndex={Binding Z}>
    <Grid>
        // an shape is displayed here...
    </Grid>
</Canvas>
Run Code Online (Sandbox Code Playgroud)

我现在希望ZIndex绑定到各个项目的Z属性.当我调试代码时,我也可以看到,当我期望它时,访问Z属性getter(每当我为它举起propertychanged事件时),所以我假设绑定工作正常.

但是,ZIndex没有按预期工作.绑定到该值对实际显示的Z Order没有影响.这个代码我哪里错了?

Fre*_*lad 10

的内容DataTemplate被包裹在一个ContentPresenter这样CanvasDataTemplate不的直接孩子ItemsPanel Grid.这就是ZIndex房产没有做任何事情的原因.

移动ZIndex BindingItemContainerStyle它应该工作.

<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> 
Run Code Online (Sandbox Code Playgroud)