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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:37:37+00:00 2026-05-12T10:37:37+00:00

Ok – I’m pulling my hair out over what I thought was a simple

  • 0

Ok – I’m pulling my hair out over what I thought was a simple scenario: create a custom Label for bilingual use that contained two additional properties (EnglishText, FrenchText). Currently its structured like this:

Public Class myCustomLabel
    Inherits System.Windows.Controls.Label

    Public myEnglishTextProperty As DependencyProperty = DependencyProperty.Register("myEnglishText", GetType(String), GetType(myCustomLabel), New PropertyMetadata("English", New PropertyChangedCallback(AddressOf TextChanged)))
    Public myFrenchTextProperty As DependencyProperty = DependencyProperty.Register("myFrenchText", GetType(String), GetType(myCustomLabel), New PropertyMetadata("Francais", New PropertyChangedCallback(AddressOf TextChanged)))

    Public Sub New()
        'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
        'This style is defined in themes\generic.xaml
        DefaultStyleKeyProperty.OverrideMetadata(GetType(myCustomLabel), New FrameworkPropertyMetadata(GetType(myCustomLabel)))
    End Sub

    Public Property myEnglishText() As String
        Get
            Return MyBase.GetValue(myFrenchTextProperty)
        End Get
        Set(ByVal value As String)
            MyBase.SetValue(myFrenchTextProperty, value)
        End Set
    End Property

    Public Property myFrenchText() As String
        Get
            Return MyBase.GetValue(myFrenchTextProperty)
        End Get
        Set(ByVal value As String)
            MyBase.SetValue(myFrenchTextProperty, value)
        End Set
    End Property

    Private Sub TextChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        If  DesignerProperties.GetIsInDesignMode(Me) = True Then
            Me.Content = myEnglishText
        Else
            If myUser.Language = "E" Then
                Me.Content = myEnglishText
            Else
                Me.Content = myFrenchText
            End If
        End If
    End Sub
End Class

My test window grid xaml is simple:

<Grid>
        <my:myCustomLabel myEnglishText="English Text" myFrenchText="English Text" Height="25" Width="100" Background="Aqua" Foreground="Black"/>
</Grid>

This seems to work in the development environment – changing the English and French texts change the in the design preview and it works when the app runs and the test window is opened. But only the first time – if I open the test window a second time I receive the following message:

‘myEnglishText’ property was already
registered by ‘myCustomLabel’.

I understand now that if I change the dependency property declarations to shared then this problem goes away – but that leads to a host of other problems like the callback function being required to be shared as well – and thus unable to update the Content (which needs to be instantiated with the class). All I really want is the content property to be updated in design time when the english and french labels are changed.

Is there a way around this? Or maybe are dependency properties overkill for what I need?

  • 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-12T10:37:37+00:00Added an answer on May 12, 2026 at 10:37 am

    You are registering your dependency properties as instance variables, and during the instance constructor. So they are getting registered again every time you instantiate the control, which causes an error the second time. As you have found out, dependency properties need to be static (Shared) members:

    Public Shared myEnglishTextProperty As DependencyProperty =   
      DependencyProperty.Register("myEnglishText", GetType(String), GetType(myCustomLabel),
      New PropertyMetadata("English", New PropertyChangedCallback(AddressOf TextChanged)))
    

    You probably need to call OverrideMetadata in your shared constructor (type initialiser) rather than your instance constructor as well.

    Regarding your issue with the callback needing to be shared: yes, it will be, but one of the arguments to the callback is the label instance. So you can just cast that to label and call an instance method on that:

    private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      ((MyLabel)d).TextChanged();
    }
    
    private void TextChanged()
    {
      // your code here
    }
    

    (forgive C# syntax)

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

Sidebar

Related Questions

I have a simple restful service that transforms a JAXB-anntotated beans to response XML
Have a webpage that will be viewed by mainly IE users, so CSS3 is
Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I'm building a small parser that scrapes web pages and logs the data on
I need a function that will clean a strings' special characters. I do NOT
I am generating some text to be shown on a web-site, and use HttpUtility.HtmlEncode
I have a PHP application that stores PHP datetime to my MySQL table. I
I am getting notice on line 30 that twetout is undefined variable <?php $username
I'm wondering why the below code I'm trying to use to pull in a

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.