我为两个不同的绑定源创建了两个不同的类。 Model1.cs和Model2.cs。
Model1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiBindingTest
{
class Model1
{
public string str1 { get; set; }
public string str2 { get; set; }
public Model1(string one , string two)
{
str1 = one;
str2 = two;
}
}
}
Model2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiBindingTest
{
class Model2
{
public string one { get; set; }
public string two { get; set; }
public Model2 (string str1 , string str2)
{
one = str1;
two = str2;
}
}
}
我有两个其他类,我希望它们是数据源,它们被命名为vm1.cs和vm2.cs; vm1有一个类型为Model1的可观察集合,Vm2有一个可观察的类类型为model2的集合,我在其构造函数中填充数据。
Vm1.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiBindingTest
{
class vm1:NotificationObject
{
private static ObservableCollection<Model1> _testVM1;
public static ObservableCollection<Model1> TestVM1
{
get { return _testVM1; }
set { _testVM1 = value; }
}
public vm1()
{
TestVM1 = new ObservableCollection<Model1>();
TestVM1.Add(new Model1("1", "Model1"));
TestVM1.Add(new Model1("2", "Model1"));
TestVM1.Add(new Model1("3", "Model1"));
}
}
}
VM2.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiBindingTest
{
class vm2:NotificationObject
{
private static ObservableCollection<Model2> _testVM2;
public static ObservableCollection<Model2> TestVM2
{
get { return _testVM2; }
set { _testVM2 = value; }
}
public vm2()
{
TestVM2 = new ObservableCollection<Model2>();
TestVM2.Add(new Model2("1", "Model2"));
TestVM2.Add(new Model2("2", "Model2"));
TestVM2.Add(new Model2("3", "Model2"));
}
}
}
NotificationObject
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiBindingTest
{
public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
我希望将Vm1绑定到panoramaitem1和vm2的datacontext作为panoramaItem2的datacontext,这样我就可以绑定每个中的longlistselector项。但是我得到了一个xamlParseException,我做错了什么。
MainPage Xaml
<phone:PhoneApplicationPage
x:Class="MultiBindingTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MultiBindingTest"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:Panorama x:Name="mainPanorama"
Title="Binding Test Application">
<phone:PanoramaItem x:Name="ItemOne"
Header="Item One">
<phone:LongListSelector>
<phone:LongListSelector.DataContext>
<local:vm1 />
</phone:LongListSelector.DataContext>
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=str1}"/>
<TextBlock Text="{Binding Path=str2}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</phone:PanoramaItem>
<phone:PanoramaItem x:Name="ItemTwo"
Header="Item Two">
<phone:LongListSelector>
<phone:LongListSelector.DataContext>
<local:vm2 />
</phone:LongListSelector.DataContext>
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=one}" />
<TextBlock Text="{Binding Path=two}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</phone:PanoramaItem>
</phone:Panorama>
</phone:PhoneApplicationPage>
其他信息 所有类都在同一名称空间
XamlParseException
System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message=Cannot create instance of type 'MultiBindingTest.vm1' [Line: 23 Position: 33]
Source=System.Windows
LineNumber=23
LinePosition=33
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at MultiBindingTest.MainPage.InitializeComponent()
at MultiBindingTest.MainPage..ctor()
InnerException:
答案 0 :(得分:0)
将您的viewmodel类设为public:
public class vm1:NotificationObject{...}
public class vm2:NotificationObject{...}