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 7544591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:42:42+00:00 2026-05-30T08:42:42+00:00

First, my scenario. I have a ViewModel with a public property Items of type

  • 0

First, my scenario. I have a ViewModel with a public property Items of type ObservableCollection<UserControl> which contains UserControls that I want to display using <ItemsControl ItemsSource="{Binding Items}" />.

I get the UserControls using Application.Current.TryFindResource("ControlKey") as UserControl.
When every resource has a different x:Key, everything works fine. However, when I add more UserControls to Items with the same x:Key, the ItemsControl displays only one of the UserControls with identical x:Key even though all of the UserControls are present in Items.

In example, I add items to the collection like so:

Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
Items.Add(Application.Current.TryFindResource("Filter2") as UserControl);

Only two controls show up in the ItemsControl, one with x:Key “Filter1” and the one with “Filter2”. The second UserControl with x:Key “Filter1” is not shown.

What am I missing? Thanks a lot.

  • 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-30T08:42:43+00:00Added an answer on May 30, 2026 at 8:42 am

    Each element in a logical tree can be used only once. You are trying to use the same element twice. Check out the following example:

    <Window x:Class="SO.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <Button x:Key="btn">Hello</Button>
        </Window.Resources>
        <StackPanel>
            <ContentControl Content="{StaticResource btn}" />
            <ContentControl Content="{StaticResource btn}" />
        </StackPanel>
    </Window>
    

    If you try to run this code, you’ll get an exception, during the assignment of the Content property. The inner exception will tel you that the element is already placed somewhere else in the tree.

    You should create a new UserControl instance for each item in the collection (hint: the keyword ‘new’ has to be used somewhere).

    EDIT (reply to @Jan comment): @Jan, while using reflection you could create new instances of object of a given type – this isn’t the preferred design. Forget about putting instances of your User Control in the application dictionary. Just have the classes defined. Then instead of:

    Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
    Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
    

    just do:

    Items.Add( new Filter1() );
    Items.Add( new Filter1() );
    

    If you have is the string ‘Filter1’ as a variable – implement a utility method ‘instantiate’ – within that function either have a switch statement, or use reflection:

    Items.Add( instantiate( key ) );
    
    private UserControl instantiate( string key ) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain my scenario first: I have around 2000 tests to run, which
I have a rather curious scenario, which I am sure that the immense talent
I'm facing a dilemma (!). In a first scenario, I implemented a solution that
I have a quite simple scenario that I cannot get to work correctly. I
I have this scenario: I have a chain of query handlers, the first is
Scenario: I have a Javascript-generated web page that someone else wrote a long time
I've a question about Entity Framework. In a code first scenario I have a
I have a strange problem with acceptance tests, capybara showing me only first scenario
I have a custom control which is having a dependency property defined in it
First the scenario: I have a list where each item has a photo of

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.