4
XAML中的DataTemplate可以与嵌套类关联吗?DataTemplate可以绑定到嵌套类吗?
我正在处理MVVM应用程序,并且遇到了数据模板问题。我有一个视图模型,为项目控件提供了其他视图模型的集合。这些项目是在外部视图模型中定义为嵌套类的层次结构的一部分。到目前为止,我一直无法在XAML中创建映射来引用内部嵌套类。
这里是类层次结构(简化为简洁起见):
public class MainViewModel
{
public class A
{
}
public class B : A
{
}
public class C : A
{
}
public ObservableCollection<A> Items
{
get;
set;
}
}
在XAML中,我试图映射一个DataTemplate,以B型和C,但我不能完全限定嵌套类的名称。
<ItemsControl ItemsSource="{Binding Path=Items}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type ns:BracingViewModel.B}">
<Grid>
....
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type ns:BracingViewModel.C}">
<Grid>
....
</Grid>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
问题:对嵌套类的引用显示为XAML中的构建错误。我得到如下:
Error 5 Cannot find the type 'ns:B'. Note that type names are case sensitive. Line...
Error 5 Cannot find the type 'ns:C'. Note that type names are case sensitive. Line...
如果我移动A,B,C类层次的MainViewModel类(即命名空间级别)之外,这工作正常。
按照一般的习惯,我尽量保持相关被定义为在其内嵌套类视图模型类,但是这使我这个问题。
所以,我的问题:是它甚至有可能到一个DataTemplate与嵌套类关联?如果是这样,XAML部分的工作如何?
在此先感谢... 乔
工程就像一个魅力,但我马上遇到的问题VS2010与WPF DESIG在使用时不能显示表格。但是,绑定确实工作正常。 –
如何对上帝的地球是任何人的预期要弄清楚这个语法?应该有一项禁止微软设计和实施API的法律。 –