question

njsokalski avatar image
0 Votes"
njsokalski asked ·

Converting a Regular ReadOnly Property to a ReadOnly DependencyProperty

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.

windows-uwp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, does the method help? Have you solved your issue?

0 Votes 0 ·
gekka avatar image
0 Votes"
gekka answered ·

Hi @njsokalski ,

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>
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

njsokalski avatar image
0 Votes"
njsokalski answered ·

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

https://docs.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?

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The content of your link is for WPF , it not for UWP.

Source of the binding does not have to be writable. But, target of the binding must be writable.
The read-only property cannot be written. So its own properties are not visible in XAML for binding.

Do you want to do a binding like OnewayToSource in WPF binding?

0 Votes 0 ·