0

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

12
  • 1
    You probably forgot to set the DataContext of the MessagingControl (or one of its parent elements) to an instance of the view model class that has the Message property. Commented Mar 8, 2016 at 12:09
  • Looks to me like you're only binding in one direction. If you want user input in the textbox to update the DP, you'll need two-way binding. BindingMode.TwoWay Commented Mar 8, 2016 at 12:16
  • @Okuma.Scott The TextBox.Text property binds two-way by default. Commented Mar 8, 2016 at 12:17
  • Are you missing the UpdateSourceTrigger=PropertyChanged which triggers when change in Textproperty. By default, it will update only on LostFocus Commented Mar 8, 2016 at 12:21
  • @Gopichandar Although it might make sense to set UpdateSourceTrigger=PropertyChanged, that does not explain why "the TextBox won't change its Text" when the Message property changes. Commented Mar 8, 2016 at 12:24

1 Answer 1

2

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; } } 
Sign up to request clarification or add additional context in comments.

5 Comments

I use the ServiceLocator (with Unity) for DataContext / Object Binding, because ViewFirst is overdressed for my purpose. But adding PropertyChanged doesn't worked too.
@Daniel check your Ourput window of VS for any Binding Errors .
Changing to FrameworkPropertyMetadata just worked, but why? Thanks!
@Daniel Changing the FrameworkPropertyMetadata is not the case why the code started working. There should be some other thing.
@Daniel Feel free to accept the answer if it is useful to resolve your problem. Thanks.