Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a DataTemplate be tied to a nested class?
    primarykey
    data
    text
    <p>Can a DataTemplate in XAML be associated with a nested class?</p> <p>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.</p> <p>Here is the class hierarchy (simplified for brevity):</p> <pre><code>public class MainViewModel { public class A { } public class B : A { } public class C : A { } public ObservableCollection&lt;A&gt; Items { get; set; } } </code></pre> <p>In XAML, I'm trying to map a DataTemplate to types B and C, but I cannot fully qualify the nested class name.</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Path=Items}"&gt; &lt;ItemsControl.Resources&gt; &lt;DataTemplate DataType="{x:Type ns:BracingViewModel.B}"&gt; &lt;Grid&gt; .... &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type ns:BracingViewModel.C}"&gt; &lt;Grid&gt; .... &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.Resources&gt; &lt;/ItemsControl&gt; </code></pre> <p>The problem : the references to the nested classes show up as a build error in the XAML. I get the following:</p> <pre><code>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... </code></pre> <p>If I move the A,B,C class hierarchy outside the MainViewModel class (i.e. to the namespace level), this works fine. </p> <p>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.</p> <p>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?</p> <p>Thanks in advance... Joe</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload