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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:18:07+00:00 2026-05-31T04:18:07+00:00

I’m having an issue where I have made a user control with two content

  • 0

I’m having an issue where I have made a user control with two content collections in it. for simplicities sake we’ll say its two items controls.

in the code behind I am exposing those itemCollections so that I can actually declare the content in another control.

for example

<!-- User Control xaml -->
<UserControl>
   <StackPanel Orientation="Horizontal" >
       <ItemsControl x:Name="_itemsControl1" />
       <ItemsControl x:Name="_itemsControl2" />
   </StackPanel>
</UserControl>


//in the codebehind for user control
public partial class TwoControls 
{
    public ItemCollection ItemsOne { get { return _itemsControl1.Items; }}
    public ItemCollection ItemsTwo { get { return _itemsControl2.Items; }}
}


<!-- Using the control in xaml later -->
<Custom:TwoControls>    
    <Custom:TwoControls.ItemsOne>
        <TextBox />
        <TextBox />
        <TextBox />
        <TextBox />
        <TextBox />
    </Custom:TwoControls.ItemsOne>
    <Custom:TwoControls.ItemsTwo>
        <Button />
        <Button />
        <Button />
        <Button />
        <Button />
    </Custom:TwoControls.ItemsTwo>
<Custom:TwoControls>

This actually works great with one small problem. As soon as I try to name any of the controls I get the following error.

<!-- Using the control in xaml later -->
<Custom:TwoControls>    
    <Custom:TwoControls.ItemsOne>
        <TextBox x:Name="txt"/>

Cannot set Name attribute value ‘txt’ on element ‘TextBox’. ‘TextBox’ is under the scope of element ‘TwoControls’, which already had a name registered when it was defined in another scope.

If I didn’t actually have to name the controls I wouldn’t. We have some tools that run expecting certain content controls to be named so as part of the build process I need them to have names. Its also worth noting that I actually have a couple of events tied up in my TwoControls class, if I were to extract that to a data template I think I could make it work but I would have to work at it a bit more than currently.

Any input on why this is would be great.

  • 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-31T04:18:09+00:00Added an answer on May 31, 2026 at 4:18 am

    I do not want to sound blunt, but whenever I got this particular namescope problem, I remember I was doing things more the Winforms way than the WPF way. I use the Usercontrols more for pagelike controls than for containerlike controls.

    A suggestion might be to create a custom control instead of a usercontrol.

        public class TwoControls: Control
        {
          static TwoControls( )
          {
            DefaultStyleKeyProperty.OverrideMetadata( typeof( TwoControls ) , new FrameworkPropertyMetadata( typeof( TwoControls ) ) );
          }
    
          public ObservableCollection<UIElement> ItemsOne
          {
            get { return ( ObservableCollection<UIElement> )GetValue( ItemsOneProperty ); }
            set { SetValue( ItemsOneProperty , value ); }
          }
          public static readonly DependencyProperty ItemsOneProperty = DependencyProperty.Register( 
            "ItemsOne" , typeof( ObservableCollection<UIElement> ) , typeof( TwoControls ) , 
            new PropertyMetadata( new ObservableCollection<UIElement>( ) ) );
    
          public ObservableCollection<UIElement> ItemsTwo
          {
            get { return ( ObservableCollection<UIElement> )GetValue( ItemsTwoProperty ); }
            set { SetValue( ItemsTwoProperty , value ); }
          }
          public static readonly DependencyProperty ItemsTwoProperty = DependencyProperty.Register( 
            "ItemsTwo" , typeof( ObservableCollection<UIElement> ) , typeof( TwoControls ) , 
            new PropertyMetadata( new ObservableCollection<UIElement>( ) ) );
        }
          //The next in your ResourceDictionary
         <Style xmlns:local="clr-namespace:WPFApplication1" TargetType="{x:Type local:TwoControls}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type local:TwoControls}">
                <StackPanel Orientation="Horizontal">
                  <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ItemsOne}"/>
                  <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ItemsTwo}"/>
                </StackPanel>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
    
        //The last in your xaml
        <Custom:TwoControls x:Name="twoControls1">
          <Custom:TwoControls.ItemsOne>
            <TextBox x:Name="Test1"/>
            <TextBox x:Name="Test2"/>
            <TextBox x:Name="Test3"/>
            <TextBox x:Name="Test4"/>
            <TextBox x:Name="Test5"/>
          </Custom:TwoControls.ItemsOne>
          <Custom:TwoControls.ItemsTwo>
            <Button x:Name="ButtonTest1"/>
            <Button x:Name="ButtonTest2"/>
            <Button x:Name="ButtonTest3"/>
            <Button x:Name="ButtonTest4"/>
            <Button x:Name="ButtonTest5"/>
          </Custom:TwoControls.ItemsTwo>
        </Custom:TwoControls>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.