¿Cómo implementar el enlace bidireccional en una propiedad?
Sé que hay muchas preguntas sobre las propiedades de dependencia, y he examinado muchas de ellas, pero ninguna parece resolver mi problema.
Tengo una ventana como esta:
<Window x:Class="WpfBindingTest.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:WpfBindingTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<local:TextInputWrapper MyText="{Binding MyTextValue, Mode=TwoWay}" />
<TextBox Text="{Binding MyTextValue, Mode=TwoWay}"/>
</StackPanel>
</Window>
Donde MyTextValue es solo una propiedad de cadena que notifica cuando se cambia:
private string _myTextValue = "Totally different value";
public string MyTextValue { get { return _myTextValue; } set { _myTextValue = value; OnPropertyChanged(); } }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
TextInputWrapper también es bastante simple:
<UserControl x:Class="WpfBindingTest.TextInputWrapper"
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:WpfBindingTest"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBox Text="{Binding MyText}"></TextBox>
</UserControl>
Código detrás:
public partial class TextInputWrapper : UserControl
{
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText",
typeof(string), typeof(TextInputWrapper), new PropertyMetadata("Empty"));
public TextInputWrapper()
{
InitializeComponent();
}
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
}
Ahora, por lo que he entendido, mi ventana ahora debe tener 2 controles TextBox que están vinculados entre sí. Como en el caso de que cambie el valor en uno, el otro debería actualizarse.
Sin embargo, termino con 2 cuadros de texto separados donde el primero comienza con el texto "Vacío", y el siguiente tiene el texto "Valor totalmente diferente". Me gusta esto:
Y cambiar el texto en cualquiera de ellos no se reproduce en el otro.
Esperaría que ambos comiencen con el texto "Valor totalmente diferente" y estén sincronizados con sus valores (al propagar los cambios a la propiedad MyTextValue en MainWindow, y al notificar el cambio allí, el cambio luego se propagaría al otro caja de texto). ¿Qué me falta para implementar correctamente el enlace de datos en mi control?