Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm new in WPF, so please sorry about my inexperience, I hope, my Question is not too stupid, but I have a problem:
I wrote a simple WPF-UserControl, which has a Lable, an Icon and a TextBox. And of course, the text of the Textbox should be bindable in both directions. It works fine when loading the control, but not, when closing it. I think I forgot to write a peace of code that sends the entered Text back to the Property Text and forces a Changed Event to outside, but I don't know how.
Can anyone help me?

Here is my Code-Behind File:
C#
public partial class TextCtrl : UserControl
{
...

public static readonly DependencyProperty TextProperty =
         DependencyProperty.Register("Text", typeof(string), typeof(TextCtrl),
         new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,  OnTextChanged));

       public string Text
       {
           get { return (string)GetValue(TextProperty); }
           set { SetValue(TextProperty, value); }
       }
       private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
       {
           TextCtrl tx = (TextCtrl)sender;
           tx.txtText.Text = eventArgs.NewValue.ToString();
       }


In the XAML where I use the Control, I Set a Binding to the Property Text:
XML
<src:TextCtrl Text="{Binding Path=.Strasse}"/>

Strasse is a Property from my ViewModel.

Thanks a lot for helping me, br Alex Hahn
Posted
Updated 20-Apr-10 22:32pm
v3

Looks to me like you forgot to put Mode=TwoWay into your XAML snippet.



 
Share this answer
 
v2
Hi!

Thanks for your Answer!
The Mode is TwoWay by default, but also when he is set explicit, it does not work!
Of course not, because the Text entered is in TextCtrl.txtText.Text, the binding is set on TextCtrl.Text!
When Loading the Control, TextCtrl.txtText.Text is set in in OnTesxtChange(..).
What i need is a possibility to get the Text from TextCtrl.txtText.Text into TextCtrl.Text.

At the Moment I do this with an EventHandler on OnTextChanged like:

<pre lang="cs">public TextCtrl()
{
InitializeComponent();
txtText.TextChanged += new TextChangedEventHandler(txtText_TextChanged);
}
void txtText_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox)
{
if (this.Text.CompareTo((sender as TextBox).Text) != 0)
{
this.Text = (sender as TextBox).Text;
}
}
}</pre>



but I think, there is a better solution. Anny Idea?

br from Austria
Alex
 
Share this answer
 
Hi again!

I found the solution: I forgot to set the DataContext of my TextBox ;P !

In the Constructor I added this:
<pre lang="cs">
public TextCtrl()
{
InitializeComponent();
TxtText.DataContext = this;
}
</pre>


Now it works fine!

Good luck with WPF to all and best regards
Alex
 
Share this answer
 

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