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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:03:21+00:00 2026-05-12T21:03:21+00:00

I tried to create my own Border class and then insert it in my

  • 0

I tried to create my own Border class and then insert it in my controls but then it seems I cannot assign names to everything inside the borders:

..
<my:ElementBorder>
        <StackPanel Name="ifBlock" Background="#E0E8FF" />
</my:ElementBorder> 
..

How can I get around this? Can I use templating somehow for that?

EDIT:
Sorry that I was unclear. Yes I subclassed Border with my own XAML file and got this compiler error with the code above:

Error 2 Cannot set Name attribute
value ‘ifBlock’ on element
‘StackPanel’. ‘StackPanel’ is under
the scope of element ‘ElementBorder’,
which already had a name registered
when it was defined in another scope.

The contents of my ElementBorder class aren’t very interesting but I’ll post it anyway:

<Border x:Class="DVPE.ElementBorder"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    BorderThickness="4" 
    CornerRadius="4">
</Border>
  • 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-12T21:03:21+00:00Added an answer on May 12, 2026 at 9:03 pm

    By introducing code-behind you have created an additional NameScope. You can remove this extra NameScope at runtime by clearing the NameScope after calling InitializeComponent():

    public ElementBorder()
    {
      InitializeComponent();
      NameScope.SetNameScope(this, null);
      ...
    }
    

    Although this will work, it is not your best solution. You would be better off creating a style that a UserControl.

    There are two ways to do this: With a subclass and without.

    With a subclass

    Create your ElementBorder subclass and override the default style. Do not call InitializeComponent():

    public class ElementBorder
    {
      static ElementBorder
      {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ElementBorder), new FrameworkPropertyMetadata(typeof(ElementBorer)));
      }
      // any additional implementation
    }
    

    In your xaml file, instead of including a tag, create a ResourceDictionary containing a style with the settings you want:

    <ResourceDictionary xmlns=... >
    
      <Style TargeType="{x:Type my:ElementBorder}">
        <Setter Property="BorderThickness" Value="4" />
        ...
    

    Merge this ResourceDictionary into the one defined in app.xaml or themes/Generic.xaml using a <MergeDictionary> tag

    Note that you can just put the style in the ResourceDictionary in your app.xaml instead of creating a separate file.

    Using this is identical to using your original ElementBorder:

    <my:ElementBorder>
      <StackPanel  ...
    

    Without a subclass

    This requires less code. Just put a Style in a ResourceDictionary as before, except give it a x:Key and use Border as it’s target type:

    <ResourceDictionary>
    
      <Style x:Key="ElementBorderStyle" TargeType="{x:Type Border}">
        <Setter Property="BorderThickness" Value="4" />
        ...
    

    This can be used as follows:

     <Border Style="{StaticResource ElementBorderStyle}">
       <StackPanel ...
    

    If you want all of your borders to have the new style, it is even easier. Just omit the x:Key and use Border as your TargetType:

    <ResourceDictionary>
    
      <Style TargeType="{x:Type Border}">
        <Setter Property="BorderThickness" Value="4" />
        ...
    

    This will cause all borders to get the style, so you can just write:

    <Border>
      <StackPanel ...
    

    Answer to additional question asked in comment below

    To set the BorderBrush the same as the Background:

      <Style TargeType="{x:Type Border}">
        <Setter Property="BorderBrush" Value="{Binding Background, RelativeSource={RelativeSource Self}}" />
        ...
    

    To set the child’s Background to the Border’s background: Generally not necessary since as long as child’s background color isn’t set, the border’s background color will show through. The only exception is in negative-Margin or RenderTransform situations where the child doesn’t render entirely inside the containing border. In that case you will need to bind the child’s Background to the Border’s Background. This can’t be done in a style applied to the BorderBrush without using an attached property. But if you’re ok doing it without a style:

    <Border x:Name="myBorder">  <!-- Style applied here -->
      <StackPanel Background="{Binding Background, ElementName=myBorder}" ...
    

    this can also be done with an unnamed border with {Binding Background, RelativeSource={RelativeSource FindAncestor,Border,1}.

    If you want to do it entirely in the style, you’ll have to add some code. Basically, create a class with an attached property named something like “MyBindingTools.BindChildBackgroundToMyBackground”. Add a PropertyChangedCallback so that when that property is set to “true”, a binding is created on the child, basically:

    BindingOperations.SetBinding(border, BackgroundProperty, new Binding("Background") { Source = this });
    

    In addition you will need to monitor the Border’s Child property so that when it changes the binding can be added to the new Child and removed from the old Child (if any).

    I wouldn’t recommend you do any of this unless you really need to, though. In your specific situation you can probably either bind the child manually or create a template that contains both the Border and the child control. Either of these would be better solutions that creating the attached property as I described.

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

Sidebar

Related Questions

I have tried to find how to create DLL-s on linux using google, but
UPDATE 3: I created a Visual Studio 2008 test project and tried to create
I am very, very new to MYSQL.I tried to create a table named option.
how to create a shortcut for a exe from a batch file. i tried
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
I tried scouring the web for help on this issue, but there are so
I tried to package a Twisted program with py2exe, but once I run the
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
I tried to install Delphi 7 on Vista several times and Vista prevented me
I tried recently to use NAnt (beta 0.86.2962.0) to run some unit tests compiled

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.