silverlight - WP7 WrapPanel & MVVM

标签 silverlight data-binding mvvm windows-phone-7

有没有办法填充 Silverlight 工具包的 WrapPanel通过绑定(bind)到 ObservableCollection ?到目前为止,我看到的所有示例,包括工具包示例本身,都填充了 WrapPanel以编程方式或通过在 XAML 中显式添加每个项目。

谢谢你的帮助!

编辑 : 关注 Geert van Horrik's advice我尝试使用 ItemsControl加载 WrapPanel通过绑定(bind)。这是 XAML:

<ScrollViewer VerticalScrollBarVisibility="Auto"
              Height="440"
              Margin="0,12,0,0">

  <ItemsControl ItemsSource="{Binding SelectionContent}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>

        <Border BorderThickness="1"
                CornerRadius="4"
                BorderBrush="{Binding BorderBrush}">

          <toolkit:GestureService.GestureListener>
            <toolkit:GestureListener Tap="OnWrapPanelTapped"
                                     DoubleTap="OnWrapPanelDoubleTapped" />
          </toolkit:GestureService.GestureListener>

          <Image Source="{Binding ImageSource}"
                 MaxHeight="48"
                 MaxWidth="48"
                 Margin="16" />
        </Border>

      </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <toolkit:WrapPanel />
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
  </ItemsControl>
</ScrollViewer>
SelectionContentObservableCollection出现在此UserControl的代码在后面。它由 SelectionItem 组成对象,实现 INotifyPropertyChanged并公开 2 个公共(public)属性 - ImageSourceBorderBrush .

我正在设置 DataContext对于UserControl在其构造函数中为 SelectionContent .但这不起作用,WrapPanel不显示任何内容。

最佳答案

您应该使用 ItemsControl。然后,您可以将 WrapPanel 设置为项目面板。

<ItemsControl ItemsSource="{Binding MyItemsSource}">
  <ItemsControl.ItemsPanel>
    <WrapPanel />
  </ItemsControl.ItemsPanel>
</ItemsControl>

关于silverlight - WP7 WrapPanel & MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4591096/

相关文章:

c# - WPF在TabControl的DataTemplate中绑定(bind)不同的UserControl

MVVM-Light:我们应该把 Messenger 的寄存器放在哪里

c# - 事件在 c# 和 silverlight 中向上传递对象链

wpf - 如何将 xml 正确绑定(bind)到 WPF DataGrid?

javascript - knockout.js 中第一列和最后一列之间具有动态列的表

c# - wpf 可以绑定(bind)到泛型类吗?

wpf - 如何在不使用扩展器的情况下将 WPF 工具栏绑定(bind)到我的 VM 中的集合

asp.net-mvc - ASP MVC中的ViewModel和Controller有什么区别

silverlight - 使用 Google Web Toolkit (GWT) 时的主要缺点是什么

wpf - Silverlight/WPF Viewmodel 最佳实践