在带有Canvas ItemsPanel的ListBox的上下文中,我需要为多个DataTemplates中的每个控件访问Cavas.ZIndex (该列表显示了几种对象类型)。仅使用
<ListBox.ItemContainerStyle>
<Setter Property="Canvas.ZIndex" ..... /> 因为有几个数据模板,每个模板都有几个控件,所以我想控制每个控件的绝对zindex。这有可能吗?
发布于 2011-11-01 00:30:28
据我所知,这是不可能的
原因是当ListBox呈现时,它呈现为这样(假设您引用的是与your other question中相同的代码):
<Canvas>
<ListBoxItem>
<ContentPresenter>
<Grid>
<TextBlock />
<Line />
</Grid>
</ContentPresenter>
</ListBoxItem>
<ListBoxItem>
<ContentPresenter>
<Grid>
<TextBlock />
<Line />
</Grid>
</ContentPresenter>
</ListBoxItem>
<ListBoxItem>
<ContentPresenter>
<Grid>
<TextBlock />
<Line />
</Grid>
</ContentPresenter>
</ListBoxItem>
...
</Canvas>如您所见,每个ListBoxItem都呈现为一组嵌套控件。您不能将所有TextBlocks绘制在所有Line的顶部,因为它们并不都共享同一父容器,而ZIndex用于对同一父容器中的项进行排序。
一种解决方法是使用两个单独的ItemsControls绘制在彼此的顶部。因此,所有的线条都将绘制在底部的ItemsControl上,而所有的TextBlocks将绘制在顶部的ItemsControl上。
<Grid>
<ItemsControl ItemsSource="{Binding MyData}"
ItemTemplate="{DynamicResource MyLineTemplate}" />
<ItemsControl ItemsSource="{Binding MyData}"
ItemTemplate="{DynamicResource MyTextBlockTemplate}" />
</Grid>https://stackoverflow.com/questions/7956082
复制相似问题