【问题标题】:change itemsource binding property mvvm更改 itemssource 绑定属性 mvvm
【发布时间】:2016-03-28 07:37:14
【问题描述】:

我在将ListView 上的ItemsSource 绑定到属性时遇到问题。 我已将我的 ListView 绑定到 viewmodel 属性:

 <ListView ItemsSource="{Binding itemgrid, Source={StaticResource viewmodel}}">
       <ListView.ItemTemplate>
            <DataTemplate>
                <local:UCListFormat></local:UCListFormat>
            </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>

我有 2 个模型,

  • 1 个项目模型

  • 2 人模型

我还有一个菜单,可以打开 1 个名为“表单”的视图,但会根据菜单名称生成 ListView

例如:

菜单人

  • 打开“表单”并生成ListViewObservableCollection&lt;person&gt;

菜单项

  • 打开“表单”并生成ListViewObservableCollection&lt;item&gt;

ListView 我声明itemgrid 为ObservableCollection&lt;object&gt;

然后我用itemgrid=ObservableCollection&lt;person&gt; 或菜单项填充它。 我用itemgrid=ObservableCollection&lt;item&gt; 填充itemgrid 基于属性。

我该怎么做?因为如果 itemgrid 是,它就不起作用 ObservableCollection&lt;object&gt;,

如果我更改itemgrid=ObservableCollection&lt;person&gt;,我在运行时只能显示1。

在后面的代码上我可以这样做

if (menu=="person")
{listview.itemsource=ObservableCollection<person>}
else
{listview.itemsource=ObservableCollection<item>}

在 XAML 上我不知道该怎么做

【问题讨论】:

  • 不清楚你在问什么,你能再写一些代码吗?
  • 对不起,你能再看看这个吗?我提供更多关于我的问题的细节......thx

标签: mvvm binding


【解决方案1】:

您可以删除显式的ListViewDataTemplate,并在资源中的某处声明 DataTemplates 以告诉 WPF 如何显示各种 VM

<DataTemplate DataType="{x:Type vm:Person}">
    <view:PersonView />
</DataTemplate>

<DataTemplate DataType="{x:Type vm:Item}">
    <view:ItemView />
</DataTemplate>

【讨论】:

  • 对不起,我真的不明白。在数据模板上,我计划使用 itemtemplate 选择器来动态更改列绑定。但我对 itemsource 绑定有问题,它如何接受 2 个模型
猜你喜欢
  • 2013-10-21
  • 2018-08-27
  • 2012-01-08
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
相关资源
最近更新 更多