mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-04 09:06:04 +02:00
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
This commit is contained in:
@@ -556,6 +556,14 @@
|
||||
{
|
||||
"source_path": "dotnet-desktop-guide/framework/wpf/properties/dependency-property-callbacks-and-validation.md",
|
||||
"redirect_url": "/dotnet/desktop/wpf/advanced/dependency-property-callbacks-and-validation?view=netframeworkdesktop-4.8"
|
||||
},
|
||||
{
|
||||
"source_path": "dotnet-desktop-guide/net/wpf/advanced/read-only-dependency-properties.md",
|
||||
"redirect_url": "/dotnet/desktop/wpf/properties/read-only-dependency-properties?view=netdesktop-6.0"
|
||||
},
|
||||
{
|
||||
"source_path": "dotnet-desktop-guide/framework/wpf/properties/read-only-dependency-properties.md",
|
||||
"redirect_url": "/dotnet/desktop/wpf/advanced/read-only-dependency-properties?view=netframeworkdesktop-4.8"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
title: "Read-only dependency properties"
|
||||
description: Learn about dependency properties in Windows Presentation Foundation (WPF) and how to create a read-only dependency property.
|
||||
ms.date: "11/29/2021"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "dependency properties [WPF], read-only"
|
||||
- "read-only dependency properties [WPF]"
|
||||
---
|
||||
<!-- The acrolinx score was 94 on 11/29/2021-->
|
||||
|
||||
# Read-only dependency properties (WPF .NET)
|
||||
|
||||
You can use read-only dependency properties to prevent property values being set from outside your code. This article discusses existing read-only dependency properties and the scenarios and techniques for creating a custom read-only dependency property.
|
||||
|
||||
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
|
||||
|
||||
## Prerequisites
|
||||
|
||||
The article assumes a basic knowledge of dependency properties, and that you've read [Dependency properties overview](dependency-properties-overview.md). To follow the examples in this article, it helps if you're familiar with Extensible Application Markup Language (XAML) and know how to write WPF applications.
|
||||
|
||||
## Existing read-only dependency properties
|
||||
|
||||
Read-only dependency properties typically report state, and shouldn't be modifiable through a `public` accessor. For example, the Windows Presentation Foundation (WPF) framework implements the <xref:System.Windows.UIElement.IsMouseOver%2A> property as read-only because its value should only be determined by mouse input. If `IsMouseOver` allowed other inputs, its value might become inconsistent with mouse input. Though not settable through a `public` accessor, many existing read-only dependency properties have values determined by multiple inputs.
|
||||
|
||||
## Uses of read-only dependency properties
|
||||
|
||||
Read-only dependency properties aren't applicable in several scenarios where dependency properties normally offer a solution. Non-applicable scenarios include data binding, applying a style to a value, validation, animation, and inheritance. However, a read-only dependency property can be used as a property trigger in a style. For example, <xref:System.Windows.UIElement.IsMouseOver%2A> is commonly used to trigger changes to the background, foreground, or other visible property of a control when the mouse is over it. The WPF property system detects and reports changes in read-only dependency properties, thus supporting property trigger functionality. Read-only dependency properties are also useful when implementing a collection-type dependency property where only the collection elements need to be writeable, not the collection object itself. For more information, see [Collection-type dependency properties](collection-type-dependency-properties.md).
|
||||
|
||||
> [!NOTE]
|
||||
> Only dependency properties, not regular common language runtime properties, can be used as property triggers in a style.
|
||||
|
||||
## Creating custom read-only dependency properties
|
||||
|
||||
Before creating a dependency property that's read-only, check the [non-applicable scenarios](#uses-of-read-only-dependency-properties).
|
||||
|
||||
The process of creating a read-only dependency property is in many ways similar to creating read-write dependency properties, with these distinctions:
|
||||
|
||||
- When registering your read-only property, call <xref:System.Windows.DependencyProperty.RegisterReadOnly%2A> instead of <xref:System.Windows.DependencyProperty.Register%2A>.
|
||||
|
||||
- When implementing the CLR property wrapper, make sure it doesn't have a public `set` accessor.
|
||||
|
||||
- `RegisterReadOnly` returns <xref:System.Windows.DependencyPropertyKey> instead of <xref:System.Windows.DependencyProperty>. Store the `DependencyPropertyKey` in a nonpublic class member.
|
||||
|
||||
You can determine the value of your read-only dependency property using whatever logic you choose. The recommended way to set the property value, either initially or as part of runtime logic, is to use the overload of <xref:System.Windows.DependencyObject.SetValue%2A> that accepts a parameter of type `DependencyPropertyKey`. Using `SetValue` is preferable to circumventing the property system and setting the backing field directly.
|
||||
|
||||
How and where you set the value of a read-only dependency property within your application will affect the access level you assign to the class member that stores the `DependencyPropertyKey`. If you only set the property value from within the class that registers the dependency property, you can use a `private` access modifier. For scenarios where the values of dependency properties affect each other, you can use paired <xref:System.Windows.PropertyChangedCallback> and <xref:System.Windows.CoerceValueCallback> callbacks to trigger value changes. For more information, see [Dependency property metadata](dependency-property-metadata.md).
|
||||
|
||||
If you need to change the value of a read-only dependency property from outside the class that registers it, you can use an `internal` access modifier for the `DependencyPropertyKey`. For example, you might call `SetValue` from an event handler in the same assembly. The following example defines an Aquarium class that calls `RegisterReadOnly` to create the read-only dependency property `FishCount`. The `DependencyPropertyKey` is assigned to an `internal static readonly` field, so that code in the same assembly can change the read-only dependency property value.
|
||||
|
||||
:::code language="csharp" source="./snippets/read-only-dependency-properties/csharp/MainWindow.xaml.cs" id="RegisterReadOnlyDependencyProperty":::
|
||||
:::code language="vb" source="./snippets/read-only-dependency-properties/vb/MainWindow.xaml.vb" id="RegisterReadOnlyDependencyProperty":::
|
||||
|
||||
Because the WPF property system doesn't propagate the <xref:System.Windows.DependencyPropertyKey> outside your code, read-only dependency properties have better write security than read-write dependency properties. Use a read-only dependency property when you want to limit write-access to those who have a reference to the `DependencyPropertyKey`.
|
||||
|
||||
In contrast, the dependency property identifier for read-write dependency properties is accessible through the property system, no matter what access modifier you assign it. For more information, see [Dependency property security](/dotnet/desktop/wpf/advanced/dependency-property-security?view=netframeworkdesktop-4.8&preserve-view=true).
|
||||
|
||||
## See also
|
||||
|
||||
- [Dependency properties overview](dependency-properties-overview.md)
|
||||
- [Implement a Dependency property](how-to-implement-a-dependency-property.md)
|
||||
- [Custom dependency properties](custom-dependency-properties.md)
|
||||
- [Collection-type dependency properties](collection-type-dependency-properties.md)
|
||||
- [Dependency property security](/dotnet/desktop/wpf/advanced/dependency-property-security?view=netframeworkdesktop-4.8&preserve-view=true)
|
||||
- [Styles and templates in WPF](/dotnet/desktop/wpf/controls/styles-templates-overview)
|
||||
+4
-3
@@ -1,11 +1,12 @@
|
||||
<Window x:Class="CodeSampleCsharp.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:CodeSampleCsharp"
|
||||
Title="Collection-Type Dependency Properties" Height="100" Width="400">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Collection-Type Dependency Properties" Height="100" Width="500">
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<Button Click="InitializeAquariums">Set collection default value in class constructor</Button>
|
||||
<Button Click="InitializeAquariums">Set collection default value in class constructor (Aquarium)</Button>
|
||||
<Button Click="InitializeFreezableAquariums">Set collection default value in class constructor (FreezableCollection Aquarium)</Button>
|
||||
|
||||
</StackPanel>
|
||||
</Window>
|
||||
|
||||
+15
-15
@@ -16,10 +16,15 @@ namespace CodeSampleCsharp
|
||||
aquarium1.AquariumContents.Add(new Fish());
|
||||
aquarium2.AquariumContents.Add(new Fish());
|
||||
MessageBox.Show(
|
||||
$"Aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
$"aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
}
|
||||
//</InitializeAquariums>
|
||||
|
||||
private void InitializeFreezableAquariums(object sender, RoutedEventArgs e)
|
||||
{
|
||||
FreezableCollectionAquarium.InitializeAquariums();
|
||||
}
|
||||
}
|
||||
|
||||
//<SetCollectionDefaultValueInConstructor>
|
||||
@@ -36,16 +41,12 @@ namespace CodeSampleCsharp
|
||||
//typeMetadata: new FrameworkPropertyMetadata(new List<FrameworkElement>())
|
||||
);
|
||||
|
||||
// Store the dependency property identifier as a static member of the class.
|
||||
public static readonly DependencyProperty AquariumContentsProperty =
|
||||
s_aquariumContentsPropertyKey.DependencyProperty;
|
||||
|
||||
// Set the default collection value in a class constructor.
|
||||
public Aquarium() => SetValue(s_aquariumContentsPropertyKey, new List<FrameworkElement>());
|
||||
|
||||
// Declare a read-only property.
|
||||
// Declare a public get accessor.
|
||||
public List<FrameworkElement> AquariumContents =>
|
||||
(List<FrameworkElement>)GetValue(AquariumContentsProperty);
|
||||
(List<FrameworkElement>)GetValue(s_aquariumContentsPropertyKey.DependencyProperty);
|
||||
}
|
||||
|
||||
public class Fish : FrameworkElement { }
|
||||
@@ -61,8 +62,8 @@ namespace CodeSampleCsharp
|
||||
aquarium2.AquariumContents.Add(new Fish());
|
||||
aquarium2.AquariumContents = new List<FrameworkElement>();
|
||||
MessageBox.Show(
|
||||
$"Aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
$"aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
}
|
||||
|
||||
//<ReadWriteDependencyProperty>
|
||||
@@ -81,7 +82,7 @@ namespace CodeSampleCsharp
|
||||
// Set the default collection value in a class constructor.
|
||||
public Aquarium() => SetValue(AquariumContentsProperty, new List<FrameworkElement>());
|
||||
|
||||
// Declare a read-write property.
|
||||
// Declare public get and set accessors.
|
||||
public List<FrameworkElement> AquariumContents
|
||||
{
|
||||
get => (List<FrameworkElement>)GetValue(AquariumContentsProperty);
|
||||
@@ -102,8 +103,8 @@ namespace CodeSampleCsharp
|
||||
aquarium1.AquariumContents.Add(new Fish());
|
||||
aquarium2.AquariumContents.Add(new Fish());
|
||||
MessageBox.Show(
|
||||
$"Aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
$"FreezableCollection aquarium1 contains {aquarium1.AquariumContents.Count} fish\r\n" +
|
||||
$"FreezableCollection aquarium2 contains {aquarium2.AquariumContents.Count} fish");
|
||||
}
|
||||
|
||||
//<FreezableCollectionAquarium>
|
||||
@@ -126,11 +127,10 @@ namespace CodeSampleCsharp
|
||||
// Set the default collection value in a class constructor.
|
||||
public Aquarium() => SetValue(s_aquariumContentsPropertyKey, new FreezableCollection<FrameworkElement>());
|
||||
|
||||
// Declare a read-only property.
|
||||
// Declare a public get accessor.
|
||||
public FreezableCollection<FrameworkElement> AquariumContents =>
|
||||
(FreezableCollection<FrameworkElement>)GetValue(AquariumContentsProperty);
|
||||
}
|
||||
|
||||
//</FreezableCollectionAquarium>
|
||||
|
||||
public class Fish : FrameworkElement { }
|
||||
|
||||
+4
-3
@@ -1,11 +1,12 @@
|
||||
<Window x:Class="CodeSampleVb.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:CodeSampleCsharp"
|
||||
Title="Collection-Type Dependency Properties" Height="100" Width="400">
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Collection-Type Dependency Properties" Height="100" Width="500">
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<Button Click="InitializeAquariums">Set collection default value in class constructor</Button>
|
||||
<Button Click="InitializeAquariums">Set collection default value in class constructor (Aquarium)</Button>
|
||||
<Button Click="InitializeFreezableAquariums">Set collection default value in class constructor (FreezableCollection Aquarium)</Button>
|
||||
|
||||
</StackPanel>
|
||||
</Window>
|
||||
|
||||
+15
-19
@@ -16,11 +16,15 @@
|
||||
Dim aquarium2 As New Aquarium()
|
||||
aquarium1.AquariumContents.Add(New Fish())
|
||||
aquarium2.AquariumContents.Add(New Fish())
|
||||
MessageBox.Show($"Aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
MessageBox.Show($"aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
End Sub
|
||||
'</InitializeAquariums>
|
||||
|
||||
Private Sub InitializeFreezableAquariums(sender As Object, e As RoutedEventArgs)
|
||||
FreezableCollectionAquarium.InitializeAquariums()
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
'<SetCollectionDefaultValueInConstructor>
|
||||
@@ -37,19 +41,15 @@
|
||||
typeMetadata:=New FrameworkPropertyMetadata())
|
||||
'typeMetadata:=New FrameworkPropertyMetadata(New List(Of FrameworkElement)))
|
||||
|
||||
' Store the dependency property identifier as a static member of the class.
|
||||
Public Shared ReadOnly AquariumContentsProperty As DependencyProperty =
|
||||
s_aquariumContentsPropertyKey.DependencyProperty
|
||||
|
||||
' Set the default collection value in a class constructor.
|
||||
Public Sub New()
|
||||
SetValue(s_aquariumContentsPropertyKey, New List(Of FrameworkElement)())
|
||||
End Sub
|
||||
|
||||
' Declare a read-only property.
|
||||
' Declare a public get accessor.
|
||||
Public ReadOnly Property AquariumContents As List(Of FrameworkElement)
|
||||
Get
|
||||
Return CType(GetValue(AquariumContentsProperty), List(Of FrameworkElement))
|
||||
Return CType(GetValue(s_aquariumContentsPropertyKey.DependencyProperty), List(Of FrameworkElement))
|
||||
End Get
|
||||
End Property
|
||||
End Class
|
||||
@@ -67,8 +67,8 @@
|
||||
aquarium1.AquariumContents.Add(New Fish())
|
||||
aquarium2.AquariumContents.Add(New Fish())
|
||||
aquarium2.AquariumContents = New List(Of FrameworkElement)()
|
||||
MessageBox.Show($"Aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
MessageBox.Show($"aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
End Sub
|
||||
|
||||
'<ReadWriteDependencyProperty>
|
||||
@@ -89,7 +89,7 @@
|
||||
SetValue(AquariumContentsProperty, New List(Of FrameworkElement)())
|
||||
End Sub
|
||||
|
||||
' Declare a read-write property.
|
||||
' Declare public get and set accessors.
|
||||
Public Property AquariumContents As List(Of FrameworkElement)
|
||||
Get
|
||||
Return CType(GetValue(AquariumContentsProperty), List(Of FrameworkElement))
|
||||
@@ -114,8 +114,8 @@
|
||||
Dim aquarium2 As New Aquarium()
|
||||
aquarium1.AquariumContents.Add(New Fish())
|
||||
aquarium2.AquariumContents.Add(New Fish())
|
||||
MessageBox.Show($"Aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"Aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
MessageBox.Show($"FreezableCollection aquarium1 contains {aquarium1.AquariumContents.Count} fish{Environment.NewLine}" +
|
||||
$"FreezableCollection aquarium2 contains {aquarium2.AquariumContents.Count} fish")
|
||||
End Sub
|
||||
|
||||
'<FreezableCollectionAquarium>
|
||||
@@ -131,19 +131,15 @@
|
||||
ownerType:=GetType(Aquarium),
|
||||
typeMetadata:=New FrameworkPropertyMetadata())
|
||||
|
||||
' Store the dependency property identifier as a static member of the class.
|
||||
Public Shared ReadOnly AquariumContentsProperty As DependencyProperty =
|
||||
s_aquariumContentsPropertyKey.DependencyProperty
|
||||
|
||||
' Set the default collection value in a class constructor.
|
||||
Public Sub New()
|
||||
SetValue(s_aquariumContentsPropertyKey, New FreezableCollection(Of FrameworkElement)())
|
||||
End Sub
|
||||
|
||||
' Declare a read-only property.
|
||||
' Declare a public get accessor.
|
||||
Public ReadOnly Property AquariumContents As FreezableCollection(Of FrameworkElement)
|
||||
Get
|
||||
Return CType(GetValue(AquariumContentsProperty), FreezableCollection(Of FrameworkElement))
|
||||
Return CType(GetValue(s_aquariumContentsPropertyKey.DependencyProperty), FreezableCollection(Of FrameworkElement))
|
||||
End Get
|
||||
End Property
|
||||
End Class
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace CodeSampleCsharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Update="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="CodeSampleCsharp.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Readonly dependency properties" Height="100" Width="400">
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<Button Click="Button_Click">Increment property value</Button>
|
||||
<Label Name="lblFishCount" Content="Aquarium fish count: 0"/>
|
||||
|
||||
</StackPanel>
|
||||
</Window>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace CodeSampleCsharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml.
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
readonly Aquarium _aquarium = new();
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Test setting a value.
|
||||
_aquarium.SetValue(Aquarium.FishCountPropertyKey, _aquarium.FishCount + 1);
|
||||
lblFishCount.Content = $"Aquarium fish count: {_aquarium.FishCount}";
|
||||
}
|
||||
}
|
||||
|
||||
//<RegisterReadOnlyDependencyProperty>
|
||||
public class Aquarium : DependencyObject
|
||||
{
|
||||
// Register a dependency property with the specified property name,
|
||||
// property type, owner type, and property metadata.
|
||||
// Assign DependencyPropertyKey to a nonpublic field.
|
||||
internal static readonly DependencyPropertyKey FishCountPropertyKey =
|
||||
DependencyProperty.RegisterReadOnly(
|
||||
name: "FishCount",
|
||||
propertyType: typeof(int),
|
||||
ownerType: typeof(Aquarium),
|
||||
typeMetadata: new FrameworkPropertyMetadata());
|
||||
|
||||
// Declare a public get accessor.
|
||||
public int FishCount =>
|
||||
(int)GetValue(FishCountPropertyKey.DependencyProperty);
|
||||
}
|
||||
//</RegisterReadOnlyDependencyProperty>
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace CodeSampleCsharp.Properties {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CodeSampleCsharp.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="CodeSampleCsharp.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:CodeSampleCsharp"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Application"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:CodeSampleVb"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
Class Application
|
||||
|
||||
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
|
||||
' can be handled in this file.
|
||||
|
||||
End Class
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
Imports System.Windows
|
||||
|
||||
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
|
||||
'1st parameter: where theme specific resource dictionaries are located
|
||||
'(used if a resource is not found in the page,
|
||||
' or application resource dictionaries)
|
||||
|
||||
'2nd parameter: where the generic resource dictionary is located
|
||||
'(used if a resource is not found in the page,
|
||||
'app, and any theme specific resource dictionaries)
|
||||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<RootNamespace>CodeSampleVb</RootNamespace>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Import Include="System.Windows" />
|
||||
<Import Include="System.Windows.Controls" />
|
||||
<Import Include="System.Windows.Data" />
|
||||
<Import Include="System.Windows.Documents" />
|
||||
<Import Include="System.Windows.Input" />
|
||||
<Import Include="System.Windows.Media" />
|
||||
<Import Include="System.Windows.Media.Imaging" />
|
||||
<Import Include="System.Windows.Navigation" />
|
||||
<Import Include="System.Windows.Shapes" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="CodeSampleVb.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Readonly dependency properties" Height="100" Width="400">
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<Button Click="Button_Click">Increment property value</Button>
|
||||
<Label Name="lblFishCount" Content="Aquarium fish count: 0"/>
|
||||
|
||||
</StackPanel>
|
||||
</Window>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
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
|
||||
@@ -94,6 +94,8 @@ items:
|
||||
href: properties/dependency-property-metadata.md
|
||||
- name: Dependency property callbacks and validation
|
||||
href: properties/dependency-property-callbacks-and-validation.md
|
||||
- name: Read-only dependency properties
|
||||
href: properties/read-only-dependency-properties.md
|
||||
- name: Common tasks
|
||||
items:
|
||||
- name: Implement a dependency property
|
||||
|
||||
@@ -378,6 +378,11 @@
|
||||
"SourceUrl": "/dotnet/desktop/wpf/advanced/dependency-property-callbacks-and-validation?view=netframeworkdesktop-4.8",
|
||||
"TargetUrl": "/dotnet/desktop/wpf/properties/dependency-property-callbacks-and-validation?view=netdesktop-6.0"
|
||||
},
|
||||
{
|
||||
"Redirect": "TwoWay",
|
||||
"SourceUrl": "/dotnet/desktop/wpf/advanced/read-only-dependency-properties?view=netframeworkdesktop-4.8",
|
||||
"TargetUrl": "/dotnet/desktop/wpf/properties/read-only-dependency-properties?view=netdesktop-6.0"
|
||||
},
|
||||
|
||||
// Systems - XAML
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user