I have followed the example in this post: Dependency Property is not updating my Usercontrol and have downloaded the example from here: http://www.mediafire.com/file/y1qvv6b68tjibh1/DependencyPropertyInsideUserControl.zip/file
What I want to do is to add a new Property onto the UserControl that has its value set from the already existing Property CellValue.
So the usercontrol is like so:
<UserControl x:Class="DependencyPropertyInsideUserControl.control"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Path = CellValue}" Name="textBox2" />
<TextBox Grid.Row="1" Text="{Binding Path = MyNewValue}" Name="myNewTextBox" /> <-- I have added this in
</Grid>
Now I want MyNewValue to get updated automatically whenever the user updates CellValue. So I have done the following in the code behind:
Added in the following:
public string MyNewValue
{
get { return (string)GetValue(MyNewValueProperty); }
set { SetValue(MyNewValueProperty, value); }
}
public static readonly DependencyProperty MyNewValueProperty =
DependencyProperty.Register("MyNewValue", typeof(string), typeof(control), new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
});
And added in a second call to SetValue(MyNewValueProperty, value) in the handler for CellValue:
public string CellValue
{
get
{
return (string)GetValue(CellValueProperty);
}
set
{
SetValue(CellValueProperty, value);
SetValue(MyNewValueProperty, value); <-- Update MyNewValueProperty whenever CellValueProperty gets updated
}
}
But it does not work, MyNewValue never changes whenever CellValue changes. I only want the user to change one value which is CellValue How can I have one property in the UserControl change when another one is modified?