Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@ 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]
> The `get` accessor for an attached property is required for data binding support in design tools, such as Visual Studio or Blend for Visual Studio.

#### 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace CodeSampleCsharp
Expand All @@ -13,6 +14,12 @@ public MainWindow()
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()
Expand All @@ -32,7 +39,7 @@ public static void SetAttachedPropertyInCode()
}

//<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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows;

namespace CodeSampleCsharp
{
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Windows Presentation Foundation (WPF) provides ways for you to design your own d
- Display specific information to users.
- Gather information from users.
- Both display and gather information.
- Display an operating system prompt, such a print window.
- Display an operating system prompt, such as print window.
- Select a file or folder.

These types of windows are known as _dialog boxes_. A dialog box can be displayed in two ways: modal and modeless.
Expand Down