I have created a custom control with a dependency property bound to a view model property.
<wpf1:CustomTextBox StringProperty="{Binding StringValue}" />
The view model looks like this
public class ViewModel : INotifyPropertyChanged
{
public string StringValue
{
get { return m_stringValue; }
set
{
m_stringValue = value;
OnPropertyChanged("StringValue");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string m_stringValue;
}
The DataContext is set in the code behind
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = ViewModel = new ViewModel();
}
public ViewModel ViewModel { get; set; }
}
The binding mode of the dependency property is two way by default
public static readonly DependencyProperty StringPropertyProperty =
DependencyProperty.Register(
"StringProperty",
typeof(string),
typeof(CustomTextBox),
new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
PropertyChangedCallback = PropertyChangedCallback
});
Now I have the problem, when the view model property StringValue is changed the custom control is notified but when I change the value of the dependency property the value of the view model is not changed.
private static void PropertyChangedCallback(DependencyObject dO,
DependencyPropertyChangedEventArgs e)
{
var textBox = (CustomTextBox) dO;
if (textBox.StringProperty == null)
return;
DoSomething(textBox.StringProperty)
textBox.StringProperty = null;
}
If I set the view model value to "some value" I want the custom control to use that string and reset it to null. So far this works, but the view model value is not synchronized and remains "some value" instead of null. Any ideas how to do this? And why the two way binding does not work that way?
Thanks.
public ViewModel ViewModel { get { return DataContext as ViewModel; } set { DataContext = value; } }
saves you any worry about keeping the typed property in sync withDataContext
. – 15ee8f99-57ff-4f92-890c-b56153