I have a very similar issue as described in this post.
I have a UserControl to encapsulate an address. This contains a number of basic controls, mostly textboxes. I then have backing dependecy properties in the code-behind for each property...
#region Line1
/// <summary>
/// Gets or sets the Line1.
/// </summary>
public string Line1
{
get
{
return (string)GetValue(Line1Property);
}
set
{
SetValue(Line1Property, value);
}
}
/// <summary>
/// The Line1 dependency property.
/// </summary>
public static readonly DependencyProperty Line1Property =
DependencyProperty.Register(
"Line1",
typeof(string),
typeof(AddressControl),
new PropertyMetadata(OnLine1PropertyChanged));
/// <summary>
/// Line1Property property changed handler.
/// </summary>
/// <param name="d">AddressControl that changed its Line1.</param>
/// <param name="e">DependencyPropertyChangedEventArgs.</param>
private static void OnLine1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as AddressControl;
if (control != null)
{
control.OnLine1Changed((string)e.OldValue, (string)e.NewValue);
}
}
/// <summary>
/// Called when the Line1 changes.
/// </summary>
/// <param name="oldValue">The old value.</param>
/// <param name="newValue">The new value.</param>
private void OnLine1Changed(string oldValue, string newValue)
{
Line1TextBox.Text = newValue;
}
#endregion Line1
I then use this control in a view...
<myControls:AddressControl Grid.Row="0" Grid.Column="3"
Line1="{Binding Path=Line1, Mode=TwoWay}"/>
This seems to set the textbox value when the view model property is updated as you would expect however my problem is getting an update from the usercontrol back to the viewmodel?
According to the link above I should check my DataContext on the control. Surley the DataContext will be the same as the parent?
I'm hoping an answer to this will apply to multiple nesting levels of controls ie. Control1 used in Control2 that is used in Control3, the whole point of reusability!
Driving me nuts so any help really grateful.