【问题标题】:Bind more than one ViewModel in View in XAML在 XAML 中的 View 中绑定多个 ViewModel
【发布时间】:2014-10-23 12:47:21
【问题描述】:

我有一个 ViewModel 类“MyViewModel”,里面有一个 ObservableCollection“MyCollection”。

然后在视图代码隐藏中我这样做是为了设置数据上下文:

this.DataContext = new MyViewModel();

在视图的前端

<Pivot ItemsSource="{Binding MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">

但是,如果我需要在此视图中使用不同的 ViewModel“MyViewModel2”和“MyCollection2”怎么办。如何跳过此步骤:

this.DataContext = new MyViewModel();

并且只在 xaml 中绑定?

我试过了,但没有任何结果:

<Pivot ItemsSource="{Binding MyViewModel.MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">

【问题讨论】:

  • 你的意思是你试图将几个不同的视图模型绑定到你视图上的不同用户控件上?
  • 我正在努力实现这一目标!
  • 每个用户控件都有自己的数据上下文 - 这应该不是问题。视图也可以有不同的数据上下文。您遇到了什么问题?

标签: c# wpf xaml mvvm windows-phone


【解决方案1】:

你最好在同一个视图模型中使用两个集合... 或者您可以参考两个子视图模型使用主 ViewModel 类;像这样:

MyViewModel 类:

public class MyViewModel
{
    public ObservableCollection<string> DataInfo { get; set; }

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}

MasterViewModel 类:

public class MasterViewModel
{
    public MyViewModel VM1 { get; set; }
    public MyViewModel VM2 { get; set; }

    public MasterViewModel()
    {
        this.VM1 = new MyViewModel();
        this.VM2 = new MyViewModel();

        VM1.DataInfo.Add("Data 1-1");
        VM1.DataInfo.Add("Data 1-2");

        VM2.DataInfo.Add("Data 2-1");
        VM2.DataInfo.Add("Data 2-2");
    }
}

查看代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MasterViewModel();
    }
}

XAML

<Window x:Class="DeleteMe4SOw.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="525">
    <StackPanel Orientation="Vertical">
        <ListView ItemsSource="{Binding VM1.DataInfo}" Height="150" Margin="10" />
        <ListView ItemsSource="{Binding VM2.DataInfo}" Height="150" Margin="10"/>
    </StackPanel>
</Window>

【讨论】:

    【解决方案2】:

    这是我的大师班:

    class CompositeViewModel
    {
        public TripsResponseTypeViewModel tripsResponseTypeViewModel { get; set; }
        public TripsViewModel tripsViewModel { get; set; }
    }
    

    在 TripsResponseTypeViewModel 我有:

    ...
        private ObservableCollection<TripsResponseType> _tripsViewModelDataSource;
        public ObservableCollection<TripsResponseType> TripsViewModelDataSource
        {
            get
            {
                if (null == this._tripsViewModelDataSource)
                {
                    this._tripsViewModelDataSource = new ObservableCollection<TripsResponseType>();
                }
    
                return this._tripsViewModelDataSource;
            }
        }
    ...
    

    在我的视图中,我想将此 TripsViewModelDataSource 设置为 Pivot 的 ItemsSource,如下所示:

    <Pivot ItemsSource="{Binding tripsResponseTypeViewModel.TripsViewModelDataSource}"...
    

    在 View.xaml.cs 中我按照你说的做了:

    this.DataContext = new CompositeViewModel();
    

    什么都没有……

    【讨论】:

    • 用工作示例中的完全复制粘贴更新了我的回复
    猜你喜欢
    • 2013-08-29
    • 2017-11-08
    • 2020-04-03
    • 2012-08-15
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多