gpt4 book ai didi

c# - Canvas 作为 ListBox ItemTemplate

转载 作者:行者123 更新时间:2023-11-30 14:38:00 26 4
gpt4 key购买 nike

我的环境是windows phone 7.1。

我有以下代码:

     <ListBox  ItemsSource="{Binding Path=Items}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Black" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="200" Height="400"
Canvas.Top="400"> <====== This is not working
... Some content ...
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>

有一个 ListBox,它有一个 Canvas 作为 ItemsPanel

ListBoxItems 本身也是 Canvas 类型。对于 ListBoxItems,我设置了 Canvas.Top =400,我希望这些项目在 ItemsPanel 中以 400 的偏移量显示。

不幸的是,这不起作用,项目以偏移量 0 呈现,如图所示(ItemsPanel 为黑色,彩色矩形是一个列表项):

enter image description here

为什么 ListBoxItems 不是以 400 的偏移量呈现的?

最佳答案

您正在 ListBoxItems 而非实际项目的内容上设置 Canvas.Top

当使用 Canvas 作为项目面板时,您必须记住您的数据模板化对象被包裹在 ListboxItems

ListBox
Canvas <- your itemtemplate
ListBoxItem
Canvas <- your datatemplate

解决方法:

<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Top" Value="400"/>
</Style>
</ListBox.ItemContainerStyle>

关于c# - Canvas 作为 ListBox ItemTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8604719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com