In WPF and other XAML-based frameworks, you can create a dependency property and assign it a value binding using the DependencyProperty class and the Binding class.
Here's an example of how to create a dependency property and assign it a value binding:
public class MyControl : Control { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyControl)); public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public MyControl() { Binding binding = new Binding("MyTextProperty"); this.SetBinding(TextProperty, binding); } } In this example, we define a MyControl class that derives from Control. We also define a static TextProperty dependency property of type string, and create a getter and setter for the Text property that uses the GetValue and SetValue methods to get and set the value of the dependency property.
In the constructor of the MyControl class, we create a new Binding object that binds to a property called "MyTextProperty", and then use the SetBinding method to set the value of the TextProperty dependency property to the binding.
When you use MyControl in your XAML markup, you can bind the Text property to a source property like this:
<Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:MyApp" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <StackPanel> <TextBox x:Name="myTextBox" Text="Hello, world!"/> <local:MyControl Text="{Binding Text, ElementName=myTextBox}"/> </StackPanel> </Window> In this example, we create a TextBox control and give it a Text property of "Hello, world!". We then create an instance of MyControl and bind its Text property to the Text property of the TextBox using the Binding markup extension.
When the XAML markup is loaded, the Text property of MyControl will be set to the value of the Text property of the TextBox, and any changes to the Text property of the TextBox will automatically be reflected in the Text property of MyControl.
"C# WPF Dependency Property Binding"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } MyProperty and how to perform binding with this property."Two-Way Binding with Dependency Property in C#"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } MyProperty dependency property."Binding to Dependency Property in XAML"
<Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <local:MyControl MyProperty="{Binding ViewModelProperty}" /> </Grid> </Window> MyProperty dependency property in XAML to a property (ViewModelProperty) in the ViewModel."Dependency Property Change Notification in C#"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata(OnMyPropertyChanged)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Handle property change logic } } OnMyPropertyChanged) to the dependency property registration."Binding to a Dependency Property with Converter"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata(OnMyPropertyChanged)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Handle property change logic } } <Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyApp" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:StringToUpperCaseConverter x:Key="StringToUpperCaseConverter" /> </Window.Resources> <Grid> <local:MyControl MyProperty="{Binding ViewModelProperty, Converter={StaticResource StringToUpperCaseConverter}}" /> </Grid> </Window> StringToUpperCaseConverter) when binding to a dependency property."Binding to Dependency Property in MVVM"
public class MyViewModel : INotifyPropertyChanged { private string _viewModelProperty; public string ViewModelProperty { get { return _viewModelProperty; } set { if (_viewModelProperty != value) { _viewModelProperty = value; OnPropertyChanged(nameof(ViewModelProperty)); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } <Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyApp" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MyViewModel /> </Window.DataContext> <Grid> <local:MyControl MyProperty="{Binding ViewModelProperty}" /> </Grid> </Window> MyViewModel)."Binding with Validation Rules in Dependency Property"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMyPropertyChanged, CoerceMyProperty)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Handle property change logic } private static object CoerceMyProperty(DependencyObject d, object baseValue) { // Add validation logic here and return the coerced value return baseValue; } } CoerceMyProperty method."Binding to Dependency Property with UpdateSourceTrigger"
public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } UpdateSourceTrigger for a two-way bound dependency property to PropertyChanged."Dependency Property Data Binding in Universal Windows Platform (UWP)"
public sealed partial class MainPage : Page { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MainPage), new PropertyMetadata(string.Empty)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } MyProperty) in the code-behind of a XAML page."Dependency Property Binding with ElementName in WPF"
<Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyApp" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="myTextBox" Text="{Binding ElementName=myControl, Path=MyProperty}" /> <local:MyControl x:Name="myControl" /> </Grid> </Window> public partial class MyControl : UserControl { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl)); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } } ElementName in WPF.mongodb-query keycode controls zipline telerik-mvc datetime-format gson zxing uisearchbardisplaycontrol