2017-08-31 95 views
0

我需要能够根据ContentControl内的子控件设置父项ContentControl的Z索引。WPF ContentControl将ZIndex设置为子ZIndex

这里是我的小例子:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
    <Rectangle Fill="Green" Width="100" Height="100" Panel.ZIndex="5"/> 
    <ContentControl> 
     <Rectangle Fill="Red" Width="100" Height="100" Panel.ZIndex="10" /> 
    </ContentControl> 
    </Grid> 
</Page> 

在这个例子中,Panel.ZIndex在RedRectangle宣称没有任何效果。

我想到了两个选项:

  • 有家长结合孩子的Panel.ZIndex
  • 让孩子设置其父Panel.ZIndex

我想不出了解如何做这两种选择。

最终我的孩子控制在一个被使用ContentControl施加DataTemplate定义,这意味着我不能直接设置的ContentControl

回答

1

Panel.ZIndex属性试试这个:

<Grid> 
    <Rectangle Fill="Green" Width="100" Height="100" Panel.ZIndex="5"/> 
    <ContentControl Panel.ZIndex="{Binding Content.(Panel.ZIndex), RelativeSource={RelativeSource Self}}"> 
     <Rectangle Fill="Red" Width="100" Height="100" Panel.ZIndex="10" /> 
    </ContentControl> 
</Grid> 
+0

我搞砸了和需要将其用于“ContentPresenter”而不是“ContentControl”。您尽管回答了我的原始问题:)(如果您知道ContentPresenter的简单方法,可随时添加评论) – ManIkWeet

+0

ContentPresenter是另一回事。它没有内容属性。但如果您有其他问题,请提出一个新问题。 – mm8