Converting a Regular ReadOnly Property to a ReadOnly DependencyProperty

Nathan Sokalski 4,121 Reputation points
2020-05-03T01:59:54.74+00:00

I have a UserControl that contains several properties that are ReadOnly (the Get method returns a generated value rather than a value that is explicitly set). I need to bind to these properties in my XAML, so I need to convert these properties to DependencyProperty(s). I have used regular DependencyProperty(s) before, but I have never used ReadOnly DependencyProperty(s). I have looked at the documentation, but found it somewhat confusing, and wasn't quite sure how convert my regular ReadOnly property (especially since the value is generated). Is there anywhere that I can see an example/comparison of a regular ReadOnly property (that generates the value) and what it would look like when converted to a ReadOnly DependencyProperty? Thanks.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. gekka 6,846 Reputation points MVP
    2020-05-03T03:52:19.087+00:00

    Hi @Nathan Sokalski ,

    The Windows Runtime does not provide a way to register a custom dependency property as read-only.

    The alternative solution are as follows.

    Public Class TestUserControl  
    	Inherits UserControl  
      
    	Public ReadOnly Property Value As Double  
    		Get  
    			Return _Value  
    		End Get  
    	End Property  
    	Private _Value As Double = 1  
      
    End Class  
      
      
    Public Class TestReadonlyUserControl  
    	Inherits UserControl  
      
    	Public Property Value As Double  
    		Get  
    			Return CType(GetValue(ValueProperty), Double)  
    		End Get  
    		Private Set(value As Double)  
    			SetValue(ValueProperty, value)  
    		End Set  
    	End Property  
      
    	Protected Shared ReadOnly ValueProperty As DependencyProperty _  
    			= DependencyProperty.Register _  
    				("Value" _  
    				, GetType(Double) _  
    				, GetType(TestReadonlyUserControl) _  
    				, New PropertyMetadata(CType(1, Double))  
    				)  
      
    	Public Sub New()  
    		Dim g As New Grid()  
    		g.Background = New SolidColorBrush(Windows.UI.Colors.Red)  
    		AddHandler g.Tapped, Sub()  
    								 Me.Value += 1  
    							 End Sub  
    		Me.Content = g  
      
    		Me.Width = 100  
    		Me.Height = 100  
    	End Sub  
    End Class  
    

    ---
    <StackPanel>
    <local:TestReadonlyUserControl x:Name="uc" />
    <TextBlock Text="{Binding ElementName=uc, Path=Value}" />
    </StackPanel>

    0 comments No comments

  2. Nathan Sokalski 4,121 Reputation points
    2020-05-03T16:04:09.133+00:00

    Maybe I was unclear about my question. The documentation I was referring to is

    https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/read-only-dependency-properties

    And here is an example of what a regular ReadOnly property might look like:

    Public ReadOnly Property FirstPlusSecond() As Integer  
    	Get  
    		Return Me.First + Me.Second  
    	End Get  
    End Property  
    

    Note that the Get returns a generated value (it does not use GetValue like a regular DependencyProperty would). I want to be able to bind to this property in my XAML. How would I make this property bindable?