Files
docs-desktop/dotnet-desktop-guide/net/wpf/properties/snippets/custom-dependency-properties/csharp/MainWindow.xaml.cs
T
Tris Shores b82ed37802 Fix naming in custom-dependency-properties snippet (#1204)
* Fix naming

* Simplify identifier assignment

* Simplify identifier assignment
2021-11-05 08:19:39 -07:00

50 lines
1.8 KiB
C#

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace CodeSampleCsharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public partial class MainWindow : Window
{
}
public class Aquarium : DependencyObject
{
//<RegisterDependencyPropertyWithWrapper>
//<RegisterDependencyProperty>
// 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))
);
//</RegisterDependencyProperty>
// Declare a read-write property.
public Uri AquariumGraphic
{
get => (Uri)GetValue(AquariumGraphicProperty);
set => SetValue(AquariumGraphicProperty, value);
}
//</RegisterDependencyPropertyWithWrapper>
private static void OnUriChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
Shape shape = (Shape)dependencyObject;
shape.Fill = new ImageBrush(new BitmapImage((Uri)e.NewValue));
}
}
}