Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Custom Control and exposing properties thru DependencyProperty
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 </code></pre> <p>My test window grid xaml is simple:</p> <pre><code>&lt;Grid&gt; &lt;my:myCustomLabel myEnglishText="English Text" myFrenchText="English Text" Height="25" Width="100" Background="Aqua" Foreground="Black"/&gt; &lt;/Grid&gt; </code></pre> <p>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:</p> <blockquote> <p>'myEnglishText' property was already registered by 'myCustomLabel'.</p> </blockquote> <p>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). <strong>All I really want is the content property to be updated in design time when the english and french labels are changed.</strong></p> <p>Is there a way around this? Or maybe are dependency properties overkill for what I need?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload