简体   繁体   English

WP7 WrapPanel和MVVM

[英]WP7 WrapPanel & MVVM

Is there a way to populate the Silverlight toolkit's WrapPanel via binding to an ObservableCollection ? 有没有一种方法可以通过绑定到ObservableCollection来填充Silverlight工具箱的WrapPanel All the examples I've seen so far, including the toolkit example itself, either populate the WrapPanel programmatically or by explicitly adding each item in XAML. 到目前为止,我看到的所有示例(包括工具箱示例本身)都可以通过编程方式填充WrapPanel或通过在XAML中显式添加每个项目来实现。

Thanks for your help! 谢谢你的帮助!

EDIT : Following Geert van Horrik's advice I tried using an ItemsControl to load the WrapPanel via binding. 编辑 :按照Geert van Horrik的建议,我尝试使用ItemsControl通过绑定加载WrapPanel This is the XAML: 这是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>

SelectionContent is an ObservableCollection present in this UserControl 's code behind. SelectionContent是此UserControl后面的代码中存在的ObservableCollection It consists of SelectionItem object, which implements INotifyPropertyChanged and exposes 2 public properties - ImageSource and BorderBrush . 它由SelectionItem对象组成,该对象实现INotifyPropertyChanged并公开2个公共属性ImageSourceBorderBrush

I'm setting the DataContext for the UserControl in its constructor to SelectionContent . 我将其构造函数中的UserControlDataContext设置为SelectionContent But this isn't working and the WrapPanel does not display anything. 但这不起作用, WrapPanel不显示任何内容。

You should use an ItemsControl. 您应该使用ItemsControl。 Then, you can set the WrapPanel as items panel. 然后,可以将WrapPanel设置为项目面板。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM