Files
docs-desktop/dotnet-desktop-guide/net/wpf/properties/snippets/read-only-dependency-properties/vb/MainWindow.xaml.vb
T
Tris Shores 5eff6608b9 Content update - Read-only dependency properties (user story 1878471) (#1230)
* Improve collection-type dp snippets that are readonly.

* Add article, snippets, toc, and redirects

* Minor edit

* Update target framework to .NET 6.0
2021-12-01 15:50:53 -08:00

48 lines
1.5 KiB
VB.net

Namespace CodeSampleVb
' <summary>
' Interaction logic for MainWindow.xaml.
' </summary>
Partial Public Class MainWindow
Inherits Window
ReadOnly _aquarium As New Aquarium()
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
' Test setting a value.
_aquarium.SetValue(Aquarium.FishCountPropertyKey, _aquarium.FishCount + 1)
lblFishCount.Content = $"Aquarium fish count: {_aquarium.FishCount}"
End Sub
End Class
'<RegisterReadOnlyDependencyProperty>
Public Class Aquarium
Inherits DependencyObject
' Register a dependency property with the specified property name,
' property type, owner type, And property metadata.
' Assign DependencyPropertyKey to a nonpublic field.
Friend Shared ReadOnly FishCountPropertyKey As DependencyPropertyKey =
DependencyProperty.RegisterReadOnly(
name:="FishCount",
propertyType:=GetType(Integer),
ownerType:=GetType(Aquarium),
typeMetadata:=New FrameworkPropertyMetadata())
' Declare a public get accessor.
Public ReadOnly Property FishCount As Integer
Get
Return GetValue(FishCountPropertyKey.DependencyProperty)
End Get
End Property
End Class
'</RegisterReadOnlyDependencyProperty>
End Namespace