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
8 changes: 8 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@
{
"source_path": "dotnet-desktop-guide/framework/wpf/properties/safe-constructor-patterns-for-dependencyobjects.md",
"redirect_url": "/dotnet/desktop/wpf/advanced/safe-constructor-patterns-for-dependencyobjects?view=netframeworkdesktop-4.8"
},
{
"source_path": "dotnet-desktop-guide/net/wpf/advanced/xaml-loading-and-dependency-properties.md",
"redirect_url": "/dotnet/desktop/wpf/properties/xaml-loading-and-dependency-properties?view=netdesktop-6.0"
},
{
"source_path": "dotnet-desktop-guide/framework/wpf/properties/xaml-loading-and-dependency-properties.md",
"redirect_url": "/dotnet/desktop/wpf/advanced/xaml-loading-and-dependency-properties?view=netframeworkdesktop-4.8"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CodeSampleCsharp
{
Expand All @@ -11,6 +9,19 @@ namespace CodeSampleCsharp
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

// Test code.
Aquarium aquarium = new();
aquarium.AquariumGraphic = new Uri("http://www.contoso.com/aquarium-graphic-new.jpg");
Debug.WriteLine($"Aquarium graphic: {aquarium.AquariumGraphic}");

// Output:
// OnUriChanged: http://www.contoso.com/aquarium-graphic-new.jpg
// Aquarium graphic: http://www.contoso.com/aquarium-graphic-new.jpg
}
}

public class Aquarium : DependencyObject
Expand All @@ -32,7 +43,7 @@ public class Aquarium : DependencyObject
);
//</RegisterDependencyProperty>

// Declare a read-write property.
// Declare a read-write property wrapper.
public Uri AquariumGraphic
{
get => (Uri)GetValue(AquariumGraphicProperty);
Expand All @@ -42,8 +53,8 @@ public Uri AquariumGraphic

private static void OnUriChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
Shape shape = (Shape)dependencyObject;
shape.Fill = new ImageBrush(new BitmapImage((Uri)e.NewValue));
Uri value = (Uri)dependencyObject.GetValue(AquariumGraphicProperty);
Debug.WriteLine($"OnUriChanged: {value}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@

Public Sub New()
InitializeComponent()

' Test code.
Dim aquarium As New Aquarium With {
.AquariumGraphic = New Uri("http://www.contoso.com/aquarium-graphic-new.jpg")
}
Debug.WriteLine($"Aquarium graphic: {aquarium.AquariumGraphic}")
End Sub

' Output:
' OnUriChanged http://www.contoso.com/aquarium-graphic-new.jpg
' Aquarium graphic: http://www.contoso.com/aquarium-graphic-new.jpg
End Class

Public Class Aquarium
Expand All @@ -30,7 +40,7 @@
propertyChangedCallback:=New PropertyChangedCallback(AddressOf OnUriChanged)))
'</RegisterDependencyProperty>

' Declare a read-write property.
' Declare a read-write property wrapper.
Public Property AquariumGraphic As Uri
Get
Return CType(GetValue(AquariumGraphicProperty), Uri)
Expand All @@ -42,8 +52,8 @@
'</RegisterDependencyPropertyWithWrapper>

Private Shared Sub OnUriChanged(dependencyObject As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim shape As Shape = CType(dependencyObject, Shape)
shape.Fill = New ImageBrush(New BitmapImage(CType(e.NewValue, Uri)))
Dim value As Uri = CType(dependencyObject.GetValue(AquariumGraphicProperty), Uri)
Debug.WriteLine($"OnUriChanged: {value}")
End Sub

End Class
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
}
}
Original file line number Diff line number Diff line change
@@ -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)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<NeutralLanguage>en-US</NeutralLanguage>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<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="XAML loading and dependency properties" Height="100" Width="400">
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Diagnostics;
using System.Windows;

namespace CodeSampleCsharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

// Test code.
Aquarium aquarium = new();
aquarium.AquariumGraphic = new Uri("http://www.contoso.com/aquarium-graphic-new.jpg");
Debug.WriteLine($"Aquarium graphic: {aquarium.AquariumGraphic}");

// Output:
// OnUriChanged: http://www.contoso.com/aquarium-graphic-new.jpg
// Aquarium graphic: http://www.contoso.com/aquarium-graphic-new.jpg
}
}

public class Aquarium : DependencyObject
{
//<DependencyPropertyWithWrapper>
// Register a dependency property with the specified property name,
// property type, owner type, and property metadata. Store the dependency
// property identifier as a public static readonly member of the class.
public static readonly DependencyProperty AquariumGraphicProperty =
DependencyProperty.Register(
name: "AquariumGraphic",
propertyType: typeof(Uri),
ownerType: typeof(Aquarium),
typeMetadata: new FrameworkPropertyMetadata(
defaultValue: new Uri("http://www.contoso.com/aquarium-graphic.jpg"),
flags: FrameworkPropertyMetadataOptions.AffectsRender,
propertyChangedCallback: new PropertyChangedCallback(OnUriChanged))
);

// Property wrapper with get & set accessors.
public Uri AquariumGraphic
{
get => (Uri)GetValue(AquariumGraphicProperty);
set => SetValue(AquariumGraphicProperty, value);
}

// Property-changed callback.
private static void OnUriChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
// Some custom logic that runs on effective property value change.
Uri newValue = (Uri)dependencyObject.GetValue(AquariumGraphicProperty);
Debug.WriteLine($"OnUriChanged: {newValue}");
}
//</DependencyPropertyWithWrapper>
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading