【问题标题】:How do I use a Silverlight2 ItemsControl to position a collection of items on a canvas?如何使用 Silverlight2 ItemsControl 在画布上定位项目集合?
【发布时间】:2009-05-04 23:51:44
【问题描述】:

在 WPF 中,您可以创建一个带有 Canvas 作为 ItemsPanel 的 ListBox,并在该画布上放置项目。执行此操作的代码如下所示:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Width="200" Height="200"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Canvas.Left" Value="{Binding Path=XPos}"/>
            <Setter Property="Canvas.Top" Value="{Binding Path=YPos}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

你能在 Silverlight2 ListBox 或者最好是 ItemsControl 中做同样的事情吗?

【问题讨论】:

    标签: silverlight listbox canvas silverlight-2.0 itemscontrol


    【解决方案1】:

    我找到了一个解决方案,但是(对我来说)它闻起来很臭。

    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Canvas Width="200" Height="200">
                    <TextBlock 
                        Text="{Binding Path=Name}" 
                        Canvas.Left="{Binding Path=XPos}" 
                        Canvas.Top="{Binding Path=YPos}" />
                </Canvas>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Width="200" Height="200"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    

    每个项目都有自己的画布,因此它们最终会堆叠在一起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多