【发布时间】:2014-02-21 00:08:14
【问题描述】:
我为两个不同的绑定源创建了两个不同的类。 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"));
}
}
}
通知对象
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 绑定为panoramaItem2 的数据上下文,这样我就可以在每个中绑定longlistselector 项。但我得到一个 xamlParseException ,我做错了什么。
主页 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:
【问题讨论】:
标签: c# silverlight windows-phone-8 windows-phone