Can a DataTemplate be tied to a nested class?

纵饮孤独 提交于 2019-12-08 16:43:59

问题


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

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

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>

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

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.

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?

Thanks in advance... Joe


回答1:


This works for me:

 <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>

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

credit to: this thread



来源:https://stackoverflow.com/questions/12806438/can-a-datatemplate-be-tied-to-a-nested-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!