C# 在XAML中绑定视图中的多个ViewModel

C# 在XAML中绑定视图中的多个ViewModel,c#,wpf,xaml,mvvm,windows-phone,C#,Wpf,Xaml,Mvvm,Windows Phone,我有一个ViewModel类“MyViewModel”,里面有一个ObservableCollection“MyCollection” 然后在视图代码中,我执行以下操作来设置数据上下文: this.DataContext = new MyViewModel(); 在视图的前端 <Pivot ItemsSource="{Binding MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="

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

然后在视图代码中,我执行以下操作来设置数据上下文:

this.DataContext = new MyViewModel();
在视图的前端

<Pivot ItemsSource="{Binding MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">
并且只绑定在xaml中

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

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

您最好在同一视图模型中使用两个集合。。。 或者,可以参照两个子ViewModel使用主ViewModel类;大概是这样的:

<Pivot ItemsSource="{Binding tripsResponseTypeViewModel.TripsViewModelDataSource}"...
MyViewModel类:

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

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}
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();
    }
}
查看隐藏代码:

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

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}
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>

您最好在同一视图模型中使用两个集合。。。 或者,可以参照两个子ViewModel使用主ViewModel类;大概是这样的:

<Pivot ItemsSource="{Binding tripsResponseTypeViewModel.TripsViewModelDataSource}"...
MyViewModel类:

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

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}
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();
    }
}
查看隐藏代码:

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

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}
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>

这是我的主修课:

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;
        }
    }
...

什么都没有。

这是我的硕士班:

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;
        }
    }
...

什么都没有。

你的意思是你试图将几个不同的视图模型绑定到视图上的不同用户控件上吗?我正试图实现这一点!每个用户控件都有自己的数据上下文——这不应该成为问题。视图也可以具有不同的数据上下文。您遇到的问题是什么?您的意思是您试图将几个不同的视图模型绑定到视图上的不同用户控件吗?我正试图实现这一点!每个用户控件都有自己的数据上下文——这不应该成为问题。视图也可以具有不同的数据上下文。您遇到了什么问题?使用工作样本的完全复制粘贴更新了我的回复使用工作样本的完全复制粘贴更新了我的回复