I'm working on a "simple" case. I like to create a new custom control which implements a DependencyProperty. In the next step I like to create a binding for updating the properties in both directions. I've builded a simple sample for this case, but the binding doesn't seem to work. I've found a way for updating the DPControl's property by using the FrameworkPropertyMetadata, but I don't know whether it's also a good idea to use the OnPropertyChanged event.
HERE is my sample project:
My control contains simply a Label
<UserControl x:Class="WPF_MVVM_ListBoxMultiSelection.DPControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPF_MVVM_ListBoxMultiSelection" mc:Ignorable="d" Height="84.062" Width="159.641"> <Grid Margin="0,0,229,268"> <Label Content="TEST" x:Name="label" Margin="0,0,-221,-102"/> </Grid> </UserControl> and implement a custom dependency property. Currently, I have also implemented the PropertyChanged method for the FramePropertyMetadata and set in this method the label's content, but I like to get it work in both directions.
public partial class DPControl : UserControl { public DPControl() { InitializeComponent(); } public string MyCustomLabelContent { get { return (string)GetValue(MyCustomLabelContentProperty);} set { SetValue(MyCustomLabelContentProperty, value); } } private static void OnMyCustomLabelContentPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { DPControl control = (DPControl)source; control.label.Content = e.NewValue; } public static readonly DependencyProperty MyCustomLabelContentProperty = DependencyProperty.Register( "MyCustomLabelContent", typeof(string), typeof(DPControl), new FrameworkPropertyMetadata(null, OnMyCustomLabelContentPropertyChanged ) ); I use this control simply in a Window by:
<local:DPControl MyCustomLabelContent="{Binding MyLabelContent, Mode=TwoWay}" Margin="72,201,286,34"/> MyLabelContent is a property in the ViewModel, which has implemented also the INotifyPropertyChanged interface.
public class ViewModel_MainWindow:NotifyPropertyChanged { private string _myLabelContent; public string MyLabelContent { get { return _myLabelContent; } set { _myLabelContent = value; RaisePropertyChanged(); } }... So how can I get it work: Using the binding feature with my new control on custom properties.