Content update - Add attached property snippet tests (user story 1878471) (#1224)

* Add snippet test and update type

* Add snippet test and update type

* Update to .NET to 6.0

* Update accessor signatures
This commit is contained in:
Tris Shores
2021-12-08 16:26:45 -08:00
committed by GitHub
parent 023dac5e24
commit 71675bec48
5 changed files with 41 additions and 13 deletions
@@ -95,9 +95,9 @@ The following example shows how to register a dependency property using the <xre
#### The Get accessor
The `get` accessor method signature is `public static object Get<property name>(object target)`, where:
The `get` accessor method signature is `public static object Get<property name>(DependencyObject target)`, where:
- `target` is the object from which the attached property is read. The `target` type can be more specific than `object`. For example, the <xref:System.Windows.Controls.DockPanel.GetDock%2A?displayProperty=nameWithType> accessor method types the `target` as <xref:System.Windows.UIElement> because the attached property is intended to be set on `UIElement` instances.
- `target` is the <xref:System.Windows.DependencyObject> from which the attached property is read. The `target` type can be more specific than `DependencyObject`. For example, the <xref:System.Windows.Controls.DockPanel.GetDock%2A?displayProperty=nameWithType> accessor method types the `target` as <xref:System.Windows.UIElement> because the attached property is intended to be set on `UIElement` instances. `UiElement` indirectly derives from `DependencyObject`.
- The return type can be more specific than `object`. For example, the <xref:System.Windows.Controls.DockPanel.GetDock%2A> method types the returned value as <xref:System.Windows.Controls.Dock> because the return value should be a `Dock` enumeration.
> [!NOTE]
@@ -105,9 +105,9 @@ The `get` accessor method signature is `public static object Get<property name>(
#### The Set accessor
The `set` accessor method signature is `public static void Set<property name>(object target, object value)`, where:
The `set` accessor method signature is `public static void Set<property name>(DependencyObject target, object value)`, where:
- `target` is the object on which the attached property is written. The `target` type can be more specific than `object`. For example, the <xref:System.Windows.Controls.DockPanel.SetDock%2A> method types the `target` as <xref:System.Windows.UIElement> because the attached property is intended to be set on <xref:System.Windows.UIElement> instances.
- `target` is the <xref:System.Windows.DependencyObject> on which the attached property is written. The `target` type can be more specific than `DependencyObject`. For example, the <xref:System.Windows.Controls.DockPanel.SetDock%2A> method types the `target` as <xref:System.Windows.UIElement> because the attached property is intended to be set on <xref:System.Windows.UIElement> instances. `UiElement` indirectly derives from `DependencyObject`.
- The `value` type can be more specific than `object`. For example, the <xref:System.Windows.Controls.DockPanel.SetDock%2A> method requires a <xref:System.Windows.Controls.Dock> value. The XAML loader needs to be able to generate the `value` type from the markup string that represents the attached property value. So, there must be type conversion, value serializer, or markup extension support for the type you use.
### Attached property attributes
@@ -1,4 +1,5 @@
using System.Windows;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace CodeSampleCsharp
@@ -13,6 +14,12 @@ namespace CodeSampleCsharp
InitializeComponent();
SetAttachedPropertyInCode();
// Test get/set accessors.
Aquarium aquarium = new();
Aquarium.SetHasFish(aquarium, true);
bool hasFish = Aquarium.GetHasFish(aquarium);
Debug.WriteLine($"Has fish: {hasFish}");
}
public static void SetAttachedPropertyInCode()
@@ -32,7 +39,7 @@ namespace CodeSampleCsharp
}
//<RegisterAttachedProperty>
public class Aquarium : DependencyObject
public class Aquarium : UIElement
{
// Register an attached dependency property with the specified
// property name, property type, owner type, and property metadata.
@@ -10,6 +10,12 @@
InitializeComponent()
SetAttachedPropertyInCode()
' Test get/set accessors.
Dim aquarium As New Aquarium()
aquarium.SetHasFish(aquarium, True)
Dim hasFish As Boolean = aquarium.GetHasFish(aquarium)
Debug.WriteLine($"Has fish: {hasFish}")
End Sub
Public Shared Sub SetAttachedPropertyInCode()
@@ -30,7 +36,7 @@
'<RegisterAttachedProperty>
Public Class Aquarium
Inherits DependencyObject
Inherits UIElement
' Register an attached dependency property with the specified
' property name, property type, owner type, and property metadata.
@@ -1,5 +1,5 @@
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows;
namespace CodeSampleCsharp
{
@@ -8,11 +8,20 @@ namespace CodeSampleCsharp
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
public MainWindow()
{
InitializeComponent();
// Test get/set accessors.
Aquarium aquarium = new();
Aquarium.SetHasFish(aquarium, true);
bool hasFish = Aquarium.GetHasFish(aquarium);
Debug.WriteLine($"Has fish: {hasFish}");
}
}
//<RegisterAttachedProperty>
public class Aquarium : DependencyObject
public class Aquarium : UIElement
{
// Register an attached dependency property with the specified
// property name, property type, owner type, and property metadata.
@@ -8,13 +8,19 @@
Public Sub New()
InitializeComponent()
' Test get/set accessors.
Dim aquarium As New Aquarium()
Aquarium.SetHasFish(aquarium, True)
Dim hasFish As Boolean = Aquarium.GetHasFish(aquarium)
Debug.WriteLine($"Has fish: {hasFish}")
End Sub
End Class
'<RegisterAttachedProperty>
Public Class Aquarium
Inherits DependencyObject
Public Class Aquarium
Inherits UIElement
' Register an attached dependency property with the specified
' property name, property type, owner type, and property metadata.