从 DataTemplate 访问 ItemsPanel 属性

发布于 2024-12-13 02:33:09 字数 278 浏览 1 评论 0原文

在带有 Canvas ItemsPanel 的 ListBox 的上下文中,我需要访问多个 DataTemplate 中每个控件的 Cavas.ZIndex(列表显示多种对象类型)。使用 a 是不够的

<ListBox.ItemContainerStyle>
    <Setter Property="Canvas.ZIndex" ..... />  

,因为有多个数据模板,每个模板都有多个控件,我想控制每个控件的绝对 zindex。 这可能吗?

In the context of a ListBox with Canvas ItemsPanel I need to access Cavas.ZIndex for each control within the multiple DataTemplates (the list displays several object types). Its not enough to use a

<ListBox.ItemContainerStyle>
    <Setter Property="Canvas.ZIndex" ..... />  

as there are several data templates each with several controls and I would like to control the absolute zindex of each control.
Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-12-20 02:33:09

据我所知,这是不可能的

原因是当 ListBox 呈现时,它会像这样呈现(假设您指的是 你的其他问题):

<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都呈现为一组嵌套控件。您不能将所有 TextBlock 绘制在所有 Line 之上,因为它们并不都共享相同的父容器,并且 ZIndex 用于对同一父容器内的项目进行排序。

解决方法是使用两个相互重叠的单独的 ItemsControl。因此,所有线条都将绘制在底部 ItemsControl 上,而所有 TextBlock 将绘制在顶部 ItemsControl 上。

<Grid>
    <ItemsControl ItemsSource="{Binding MyData}"
                  ItemTemplate="{DynamicResource MyLineTemplate}" />

    <ItemsControl ItemsSource="{Binding MyData}"
                  ItemTemplate="{DynamicResource MyTextBlockTemplate}" />
</Grid>

To my knowledge, this is not possible

The reason is that when a ListBox renders, it renders like this (assuming you're referring to the same code you had in 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>

As you can see, each ListBoxItem is rendered as a group of nested controls. You cannot have all your TextBlocks drawn on top of all your Lines because they do not all share the same parent, and ZIndex is used to order items that are within the same parent container.

A workaround would be to use two separate ItemsControls drawn on top of each other. So all your Lines would be drawn on the Bottom ItemsControl while all the TextBlocks would be drawn on the Top ItemsControl.

<Grid>
    <ItemsControl ItemsSource="{Binding MyData}"
                  ItemTemplate="{DynamicResource MyLineTemplate}" />

    <ItemsControl ItemsSource="{Binding MyData}"
                  ItemTemplate="{DynamicResource MyTextBlockTemplate}" />
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文