user5856424
user5856424

Reputation:

TextBox string Dependency Property doesn't work

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

Upvotes: 0

Views: 1190

Answers (1)

Gopichandar
Gopichandar

Reputation: 2841

Here is the full working sample code.

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

Upvotes: 2

Related Questions