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

  • SEARCH
  • Home
  • 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 8890347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:28:50+00:00 2026-06-14T22:28:50+00:00

Since I often use TemplateSelectors that distinguish between templates based on Type, I tried

  • 0

Since I often use TemplateSelectors that distinguish between templates based on Type, I tried to write a TemplateSelector that has a property to hold the type/template relationship.

I tried to use x:Array to set this property in XAML. This does not work since VS complains that x:Array is not a class that implements IEnumerable, which according to the documentation it should

MSDN:

But x:Array can also be useful for populating certain properties using XAML that take general collection support interfaces or classes as their structured property content, for instance as IEnumerable.

Here the corresponding code lines

TemplateSelector

public class TypeMatchDataTemplateSelector : DataTemplateSelector
{
    public TypeTemplate[] Templates { get; set; }

    ...
}

public class TypeTemplate
{
    public Type Type { get; set; }
    public DataTemplate Template { get; set; }
}

Use in XAML

<x:Array Type="ct:TypeTemplate" x:Key="fooTemplates">
    <ct:TypeTemplate
        Type="{x:Type logic:NamedRegisterInformation}"
        Template="{StaticResource RegisterListTemplate}" />
    <ct:TypeTemplate
        Type="{x:Type logic:AddressedRegisterInformation}"
        Template="{StaticResource RegisterListTemplate}" />
</x:Array>

<ct:TypeMatchDataTemplateSelector
    x:Key="foo"
    Templates="{StaticResource fooTemplates}"/>
...
...
<ListBox ItemTemplate="{StaticResource blub}">

The used Types and Templates should be correct. If I execute the code I get this Exception at runtime:

Cannot convert the value in attribute ‘Templates’ to object of type
‘EP3_gui.UI.ContentTemplates.TypeTemplate[]’. Object of type
‘System.Windows.Markup.ArrayExtension’ cannot be converted to type
‘EP3_gui.UI.ContentTemplates.TypeTemplate[]’. Error at object ‘blub’
in markup file ‘EP3_gui;component/ui/readmsfrcontrol.xaml’ Line 36
Position 11.

Any hints where my error might lie?

  • 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-06-14T22:28:51+00:00Added an answer on June 14, 2026 at 10:28 pm

    Why are you making your life so hard? I mean there are many other ways to be a good programmer! I was just kidding.

    You can simply write two "DataTemplate"s and filter them based on their "DataType" like this:

    <Window.Resources>
    
        <DataTemplate DataType="x:Type logic:AddressedRegisterInformation" ></DataTemplate>
    
        <DataTemplate DataType="x:Type logic:NamedRegisterInformation" ></DataTemplate>
    
    </Window.Resources>
    <ListBox ItemsSource="{Binding}" />
    

    Why don’t you do this? instead of trying to use complicated custom "TemplateSelector" that might cause you many problems like the one you mentioned. This way WPF will do all the selection things and you sit, bind an array of "Object" to the "ItemsSource".

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created my own class that extends CountDownTimer. Since I use it often,
Since REST is stateless, each request that comes in has no knowledge of the
I have a custom class called Rows that implements IEnumerable<Row> . I often use
JQuery events are annoying me. The thing is that I very often use javascript
So I have a script that ssh into another computer. Since I use it
When I use an imperative language I often write code like foo (x) {
When writing a T-SQL script that I plan on re-running, often times I use
At school I was often taught that preprocessor statements can easily go haywire, since
I often use Octave to create data that I can plot from my lab
Since now I have only used plugin for editing and the way I use

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.