【问题标题】:Canvas as ListBox ItemTemplate画布作为 ListBox ItemTemplate
【发布时间】:2011-12-22 13:41:53
【问题描述】:

我的环境是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 类型。对于我设置了Canvas.Top =400ListBoxItems,我希望这些项目在ItemsPanel 中以400 的偏移量显示。

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

为什么ListBoxItems 没有在偏移量 400 处呈现?

【问题讨论】:

    标签: c# silverlight windows-phone-7 canvas


    【解决方案1】:

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

    当使用画布作为项目面板时,您必须记住您的数据模板对象包含在 ListboxItems

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

    解决方案:

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

    【讨论】:

    • 您的解决方案有效,但现在我意识到 wp7 上的 silverlight 不支持 ItemContainerStyle 中的绑定,aaaarrrrgh :/
    • 不,不幸的是,silverlight 中的样式不支持绑定(但)SL5 有它们,所以我相信 WP12 或其他东西也会。
    • 呵呵,我等不及了:)
    【解决方案2】:

    尝试将此添加到您的ListBox

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
    

    因为你看,黑色区域是ListBox,而不是你的ListBoxItem。由于一个“众所周知的错误”,如果我们仍然可以这样称呼它,ListBoxItem 不会拉伸,除非您添加上面的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 2014-08-16
      • 2013-02-01
      • 2012-07-29
      相关资源
      最近更新 更多