文本框字符串依赖项 属性 不起作用

TextBox string Dependency Property doesn't work

我写了一点 string 依赖 属性 用于在一个 UserControl 中绑定 TextBox 以将其绑定到另一个 UserControl 中,但它不会更新。

用户控件隐藏代码:

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); }
}

用户控件XAML:

<TextBox Text="{Binding Path=MessageTextSend,
                        RelativeSource={RelativeSource Mode=FindAncestor,
                                                       AncestorType={x:Type local:MessagingControl}}}" />

我是这样使用它的:

<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); }
}

如果我设置消息 属性,TextBox 不会更改其文本,如果通过键盘设置 TextBox 文本,它也不会更改 属性。

我会错过什么?

编辑:添加了带有 DataContext 的 UserControl 标签

这是完整的示例代码。

MessagingControl.xaml

<StackPanel>
    <TextBox Text="{Binding Path=MessageTextSend, UpdateSourceTrigger=PropertyChanged,
                    RelativeSource={RelativeSource Mode=FindAncestor,
                                                   AncestorType={x:Type local:MessagingControl}}}" />
</StackPanel>

MessagingControl.xaml.cs

    public MessagingControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MessageTextSendProperty =
       DependencyProperty.Register("MessageTextSend", typeof(string), typeof(MessagingControl),
           new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public string MessageTextSend
    {
        get { return (string)GetValue(MessageTextSendProperty); }
        set { SetValue(MessageTextSendProperty, value); }
    }

MainWindow.xaml

<StackPanel>
    <local:MessagingControl MessageTextSend="{Binding Message}"  x:Name="uc"/>        
    <TextBlock Text="{Binding ElementName=uc, Path=MessageTextSend}" />
</StackPanel>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new SampleClass() { Message = "My Message" };
    }       
}

public class SampleClass
{
    public string Message { get; set; }
}