Button myButton = new Button(); myButton.SetValue(Button.BackgroundProperty, new SolidColorBrush(Colors.Red)); // ... SolidColorBrush myBrush = (SolidColorBrush)myButton.GetValue(Button.BackgroundProperty);
public class MyCustomControl : Button { public static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.Register("IsHighlighted", typeof(bool), typeof(MyCustomControl), new PropertyMetadata(false)); public bool IsHighlighted { get { return (bool)this.GetValue(IsHighlightedProperty); } set { this.SetValue(IsHighlightedProperty, value); } } } // Usage: MyCustomControl myControl = new MyCustomControl(); myControl.IsHighlighted = true;In this example, we create a custom control that derives from the Button class. We define a new dependency property called IsHighlighted, which can be used to indicate whether the control is currently highlighted or not. The GetValue and SetValue methods are used to get and set the value for this property. Package library: Windows.UI.Xaml.