【问题标题】:Binding Observable collection to wrappanel in MVVM在 MVVM 中将 Observable 集合绑定到 wrappanel
【发布时间】:2014-05-04 10:30:41
【问题描述】:

我有一个 XYZ 类型的对象和一个视图模型 XYZViewModel。我有这个对象的数据模板

  <DataTemplate DataType="{x:Type vm:XYZViewModel}">
    <Grid>
        / some ui 
    </Grid>
 </DataTemplate>

我有一个视图模型“ListXYZViewModel”,其中包含“XYZViewModel”列表。现在我应该显示这些对象的列表。

最好的方法是什么? 我想在包装面板中显示它。我这样做了

 <ItemsPanelTemplate x:Key="WrapPanelTemplate">
    <WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>


<DataTemplate DataType="{x:Type vm:ListXYZViewModel}">
    <Grid>
        <ItemsControl ItemsSource="{Binding HomeViewModel}" ItemsPanel="{StaticResource WrapPanelTemplate }"></ItemsControl>            
    </Grid>
</DataTemplate>

这不起作用。有什么建议吗?

【问题讨论】:

  • HomeViewModel 本身是一个集合还是您的意思是将 ItemsSource 绑定到集合上?
  • HomeViewModel 是 XYZViewModel 的 ObservableCollection 对象

标签: c# wpf mvvm


【解决方案1】:

这对你有用吗?

<ItemsControl ItemsSource="{Binding HomeViewModel}" ItemsPanel="{StaticResource WrapPanelTemplate }">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type vm:ListXYZViewModel}">
           <Grid>
              <!-- your ui -->
           </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

【讨论】:

    【解决方案2】:

    尝试明确设置您的ItemTemplate

    <ItemsControl 
        ItemsSource="{Binding HomeViewModel}" 
        ItemsPanel="{StaticResource WrapPanelTemplate}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    / some ui 
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl> 
    

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多