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:
Hi,
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 :

C#
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);}
      set{SetValue(TextProperty,value);}
  }

  static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventsArgs args)
  {
      UCRTextBox ucRounded=(UCRTextBox)property;
      ucRounded.TextBx.text=(string)args.NewValue;
  }

  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:

C#
private void SetTextBoxBinding(FrameworkElement target,DependencyProperty dp,string value)
{
   Binding b=new Binding(value);
   b.source=_editPatient.Patient;
   b.Mode=BindingMode.TwoWay;
   target.SetBinding(dp,binding);
}


Using for this function is :

on the windowLoad Events

C#
this.SetTextbinding(this.txtFirstName,TextBox.TextProperty,"FirstName");



that is all .

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

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:


XML
<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:


XML
<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