Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:50:28+00:00 2026-05-16T05:50:28+00:00

This question is strongly connected to this answer to How to reference a generic

  • 0

This question is strongly connected to this answer to “How to reference a generic type in the DataType attribute of a HierarchicalDataTemplate?“

I followed the basic idea of that answer and created this data structure:

<!-- for DictItemVM<string, Remote.Address> which is a viewmodel for a KeyValuePair<...> -->
<x:Array Type="{x:Type sys:Type}"
         x:Key="KVParamsStringToRemoteAddress"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:remote="clr-namespace:Remote"
         xmlns:mvvm="clr-namespace:MVVM">
    <x:Type TypeName="sys:String" />
    <mvvm:GenericType BaseType="{x:Type TypeName=remote:Address}"/>
</x:Array>

<mvvm:GenericType xmlns:mvvm="clr-namespace:MVVM"
                  BaseType="{x:Type TypeName=mvvm:DictItemVM`2}"
                  InnerTypes="{StaticResource KVParamsStringToRemoteAddress}"
                  x:Key="DictItemVMOfStringToRemoteAddress"/>

DictItemVM<T,U> is a viewmodel for a KeyValuePair<...> and is derived from BaseVM. BaseVM has a DataTemplate view, but I’m trying hard to create one for DictItemVM<string, Remote.Address>.
Remote.Address is a complex value type (stores Path and Access information). Remote.Address has its own DataTemplate view.
So now that I have the StaticResource “DictItemVMOfStringToRemoteAddress”, I want to use it to specify a DataTemplate:

<DataTemplate x:Key="TestKey" DataType="{StaticResource DictItemVMOfStringToRemoteAddress}">
    <StackPanel>
        <Label Content="UniqueName" />
        <TextBox Text="{Binding UniqueName}" />
        <Label Content="Key"/>
        <TextBox Text="{Binding Key, Mode=OneWay}" IsEnabled="False" />
        <Label Content="Value"/>
        <ContentControl Content="{Binding Value, Mode=OneWay}" />
    </StackPanel>
</DataTemplate>

Now this DataTemplate should be used as a view, but instead the view for BaseVM is being displayed.
Someone give me a hint on this one?

[edit: 2010-08-09]
Some things I tried:

In the x:Array definition I replaced
<mvvm:GenericType BaseType="{x:Type TypeName=remote:Address}"/>
with
<x:Type TypeName="remote:Address"/>,
because that’s what it basically is – no difference.

Also tried to create the DataType in between tags (instead of linking to a StaticResource) like this:

<DataTemplate x:Key="TestKey">
    <DataTemplate.DataType>
        <Binding>
            <Binding.Source>
                <mvvm:GenericType 
                  BaseType="{x:Type TypeName=mvvm:DictItemVM`2}">
                    <mvvm:GenericType.InnerTypes>
                        <x:Type TypeName="sys:String" />
                        <x:Type TypeName="remote:Address"/>
                    </mvvm:GenericType.InnerTypes>
                </mvvm:GenericType>
            </Binding.Source>
        </Binding>
    </DataTemplate.DataType>

Tried it with and without an x:Array within the GenericType.InnerTypes, both giving me this error.

Tried to pass the type from a static property like this:
DataType="{x:Static mvvm:StaticTypes.DictItemVMOfStringToRemoteAddress}"
and like this:
DataType="{Binding Path={x:Static mvvm:StaticTypes.DictItemVMOfStringToRemoteAddress}}"
No difference.

Strange enough this specific DataTemplate needs to have some x:Key value, in contrast to all others in the xaml resource file which all point to a regular type like e.g.: DataType="{x:Type mvvm:EffectVM}". If I remove the x:Key, I get this error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:50:28+00:00Added an answer on May 16, 2026 at 5:50 am

    I found a solution, though that solution is not really satisfying.

    In XAML, create a DataTemplate for each type of KeyValuePair<T,U> you want to display and give it some unique x:Key:

    <DataTemplate x:Key="DictItemOfStringAndAddressVM">
        <!-- ... -->
    </DataTemplate>
    

    Then in codebehind, create a DataTemplateSelector and override SelectTemplate:

    public class GenericDataTemplateSelector : System.Windows.Controls.DataTemplateSelector
    {
        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;
    
            if ((element != null) && (item != null))
            {
                if (item is DictItemVM<string, Remote.Address>)
                {
                    return element.FindResource("DictItemOfStringAndAddressVM") as DataTemplate;
                }
                else if(item is SomeOtherComplexType)
                {
                    // ...
                }
                else return base.SelectTemplate(item, container);
            }
            return null;
        }
    }
    

    Again in XAML, declare this class as a resource:

    <mvvm:GenericDataTemplateSelector x:Key="GenDataTempSelect"/>
    

    Finally, (in my case) in the ContentControl, add the property:

    ContentTemplateSelector="{StaticResource GenDataTempSelect}"
    

    —

    Disadvantages:

    • When creating a new DataTemplate you have to change code at two locations.
    • Each ContentControl, ListView, … must set it’s appropriate property.
    • Doesn’t really answer the question of how to reference generic types in WPF!

    Advantages:

    • Easy to add new types of any structure or complexity (enjoying all the benefits C# has over WPF…)
    • No complicated nested type description in WPF, as the above solution would require.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly I appreciate that this question could be seen as subjective but I strongly
I originally asked this question , but in finding an answer, discovered that my
This question is really basic. What is the performance difference between removing a UIView
Very similar to this question , I am trying to convert a project that
This probably is a very very basic question but i can't seem to find
Let me preface this question by stating that I'm not a C# developer. I'm
See this question . I have the following code that executes against a SQLIte
Update: I can't delete this question, because the answer has been upvoted, yet it
Firstly, I've asked this question elsewhere , but meta.stackoverflow.com seems to think that asking
This question is directly related to this SO question I posed about 15 minutes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.