Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem if any one can help me to solve it, the problem is.
i have a user control it is Rounded Text Box,composed from one text box only,
i did write some line of code to can bind this user control,it is worked but in one way if i change the source property the rounded text box show the modification by if i change from the text box the change do not appear in property bounded;
the code behind of rounded text box is :

public partial class  UCRTextBox : UserControl
  public static readonly DependencyProperty TextProperty = DependencyProperty.Register
  ("Text",typeof(string),typof(UCRTextBox), new FrameworkPropertyMetadata  
  (null,FrameworkMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallBach(textChangedCallBack),
  new CoerceValueCallback(coerceValue)));

  public string Text
      get{return (string)GetValue(TextProperty);}

  static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventsArgs args)
      UCRTextBox ucRounded=(UCRTextBox)property;

  static object CoerceValue(DependencyObject sender,object value)
      return (string)value;


and the code in the window with i did use this control i did bind it by code behind:

private void SetTextBoxBinding(FrameworkElement target,DependencyProperty dp,string value)
   Binding b=new Binding(value);

Using for this function is :

on the windowLoad Events


that is all .

can any one show me the correct way to solve this problem??
and thx for all
Updated 11-Jan-12 4:16am

1 solution

Why don't you set the Binding from the XAML?

In your UserControl's XAML you can bind the Text property of the TextBox to the Text property of the UserControl, like the following:

<TextBox x:Name="TextBx"
            Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />

In the window that uses the UserControl you can bind the Text property of the UserControl, like the following:

<ucNamespace:UCRTextBox x:Name="TextBx"
         Text="{Binding MyProperty, Mode=TwoWay}" />

Don't forget to set the DataContext of the window appropriately. :)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900