可以将DataTemplate绑定到嵌套类吗? [英] Can a DataTemplate be tied to a nested class?

查看:114
本文介绍了可以将DataTemplate绑定到嵌套类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XAML中的DataTemplate可以与嵌套类相关联吗?

Can a DataTemplate in XAML be associated with a nested class?

我正在开发MVVM应用程序,但是遇到了数据模板问题。我有一个视图模型,为项目控件提供了其他视图模型的集合。这些项目是在外部视图模型中定义为嵌套类的层次结构的一部分。到目前为止,我一直无法在XAML中创建一个映射来引用内部嵌套类。

I am working on a MVVM application, and I have run into a data template problem. I have a view model providing a collection of other view models for an items control. These items are part of a hierarchy defined as nested classes in the outer view model. I have been unable so far to create a mapping in XAML to reference the inner nested class.

这是类层次结构(为简便起见简化):

Here is the class hierarchy (simplified for brevity):

public class MainViewModel
{
    public class A
    {
    }

    public class B : A
    {
    }

    public class C : A
    {
    }

    public ObservableCollection<A> Items
    {
        get;
        set;
    }
}

在XAML中,我试图映射一个DataTemplate到类型B和C,但是我不能完全限定嵌套的类名。

In XAML, I'm trying to map a DataTemplate to types B and C, but I cannot fully qualify the nested class name.

<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中显示为生成错误。我得到以下信息:

The problem : the references to the nested classes show up as a build error in the XAML. I get the following:

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类之外(即到名称空间级别),效果很好。

If I move the A,B,C class hierarchy outside the MainViewModel class (i.e. to the namespace level), this works fine.

通常,我尝试将与视图模型相关的类保留为嵌套类,但这会导致我遇到这个问题。

As a general habit, I try to keep classes relevant to the view model defined as nested classes within it, but that leads me to this issue.

所以,我的问题是:是否有可能将DataTemplate与嵌套类相关联?如果是这样,在XAML部分中是如何完成的?

So, my question : is it even possible to associate a DataTemplate with a nested class? If so, how is that done in the XAML portion?

预先感谢...
Joe

Thanks in advance... Joe

推荐答案

这对我有用:

 <ItemsControl ItemsSource="{Binding Path=Items}">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type ns:MainViewModel+B}">
                <Grid Background="Blue"
                      Width="30"
                      Height="30">

                </Grid>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ns:MainViewModel+C}">
                <Grid Background="Chartreuse" Width="30" Height="30">

                </Grid>
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>

换句话说,只需更改 x:Type 标记扩展名中的 +

In other words, just change the . to + in the x:Type markup extension

感谢:此线程

这篇关于可以将DataTemplate绑定到嵌套类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆