Last active
October 7, 2022 16:53
-
-
Save wi7a1ian/6c142e238e89458f70e7d8cdcb890f1c to your computer and use it in GitHub Desktop.
Example of DependencyProperty #wpf #csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static readonly DependencyProperty TopProperty = | |
DependencyProperty.RegisterAttached("Top", | |
typeof(double), typeof(Canvas), | |
new FrameworkPropertyMetadata(0d, | |
FrameworkPropertyMetadataOptions.Inherits)); | |
public static void SetTop(UIElement element, double value) | |
{ | |
element.SetValue(TopProperty, value); | |
} | |
public static double GetTop(UIElement element) | |
{ | |
return (double)element.GetValue(TopProperty); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- To set the value of an attached property, add an attribute in XAML with a prefix of the element that | |
provides the attached property. To set the the Canvas.Top and Canvas.Left property of a button aligned within | |
a Canvas panel, you write it like this: --> | |
<Canvas> | |
<Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/> | |
</Canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DependencyPropertyExample | |
{ | |
// Dependency Property | |
public static readonly DependencyProperty CurrentTimeProperty = | |
DependencyProperty.Register( "CurrentTime", typeof(DateTime), | |
typeof(MyClockControl), | |
new FrameworkPropertyMetadata( DateTime.Now, | |
OnCurrentTimePropertyChanged, // optional | |
OnCoerceCurrentTimeProperty ), // optional | |
OnValidateCurrentTimeProperty ); // optional | |
// .NET Property wrapper | |
public DateTime CurrentTime | |
{ | |
get { return (DateTime)GetValue(CurrentTimeProperty); } | |
set { SetValue(CurrentTimeProperty, value); } | |
} | |
private static void OnCurrentTimePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) | |
{ | |
MyClockControl control = source as MyClockControl; | |
DateTime time = (DateTime)e.NewValue; | |
// Put some update logic here... | |
} | |
private static object OnCoerceTimeProperty( DependencyObject sender, object data ) | |
{ | |
if ((DateTime)data > DateTime.Now ) | |
{ | |
data = DateTime.Now; | |
} | |
return data; | |
} | |
private static bool OnValidateTimeProperty(object data) | |
{ | |
return data is DateTime; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DependencyPropertyDescriptor textDescr = DependencyPropertyDescriptor. | |
FromProperty(TextBox.TextProperty, typeof(TextBox)); | |
if (textDescr!= null) | |
{ | |
textDescr.AddValueChanged(myTextBox, delegate | |
{ | |
// Add your propery changed logic here... | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// used to report the state of a control, like the IsMouseOver property. Is does not make sense to provide a setter for this value. | |
// Register the private key to set the value | |
private static readonly DependencyPropertyKey IsMouseOverPropertyKey = | |
DependencyProperty.RegisterReadOnly("IsMouseOver", | |
typeof(bool), typeof(MyClass), | |
new FrameworkPropertyMetadata(false)); | |
// Register the public property to get the value | |
public static readonly DependencyProperty IsMouseoverProperty = IsMouseOverPropertyKey.DependencyProperty; | |
// .NET Property wrapper | |
public int IsMouseOver | |
{ | |
get { return (bool)GetValue(IsMouseoverProperty); } | |
private set { SetValue(IsMouseOverPropertyKey, value); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment