I wrote a little string Dependency Property for TextBox Binding in a UserControl to bind it in another one, but it won't update.
UserControl CodeBehind:
public static readonly DependencyProperty MessageTextSendProperty =
DependencyProperty.Register("MessageTextSend", typeof(string), typeof(MessagingControl),
new PropertyMetadata(default(string)));
public string MessageTextSend
{
get { return (string)GetValue(MessageTextSendProperty); }
set { SetValue(MessageTextSendProperty, value); }
}
UserControl XAML:
<TextBox Text="{Binding Path=MessageTextSend,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:MessagingControl}}}" />
And I'm using it like this:
<UserControl x:Class="SomeProject.View.ChatView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:command="http://www.galasoft.ch/mvvmlight"
xmlns:control="clr-namespace:SomeProject.View.Control"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="clr-namespace:SomeProject.Model"
DataContext="{Binding Chat,
Source={StaticResource Locator}}"
FontSize="15"
d:DesignHeight="500"
d:DesignWidth="700"
mc:Ignorable="d">
<control:MessagingControl Grid.Column="0"
MessageBubbleCollection="{Binding MessageCollection}"
MessageTextSend="{Binding Message}"
SendCommand="{Binding SendMessageCommand}" />
</UserControl>
public string Message
{
get { return _message; }
set { Set(ref _message, value); }
}
If I'm setting the Message Property, the TextBox won't change its Text and if the TextBox Text is set via keyboard, it doesn't changes the Property too.
What could I missing?
Edit: Added UserControl Tag with DataContext
BindingMode.TwoWayTextBox.Textproperty binds two-way by default.UpdateSourceTrigger=PropertyChangedwhich triggers when change inTextproperty. By default, it will update only onLostFocusUpdateSourceTrigger=PropertyChanged, that does not explain why "the TextBox won't change its Text" when the Message property changes.