I am quite new to databinding in WPF, and I have a problem with a custom Textbox/Label and TwoWay databinding. The Text property can be set through databinding, but when the user changes the Text, the binding doesent update the source property.
First, I have my object which contains the source property:
public class DataObject { public string MyString { get; set; } } Then I have my custom UserControl. The purpose of this UserControl is bascially to show a textbox whenever it is clicked, so the user can change the value.
.... <DockPanel > <Label Name="Label" MouseLeftButtonDown="EditText" Style="{StaticResource InputLabel}" Content="{Binding Path=Text, RelativeSource={RelativeSource FindAncestor, AncestorType=MyNamespace:MyCustomLabel, AncestorLevel=1},Mode=TwoWay}" ></Label> <TextBox Name="TextBox" Style="{StaticResource TextBox}" Visibility="Collapsed" HorizontalAlignment="Left" HorizontalContentAlignment="Left" /> </DockPanel> .... MyCustomLabel.Xaml.cs:
public partial class MyCustomLabel : UserControl { private static readonly DependencyProperty TextProperty=DependencyProperty.Register("Text",typeof(string),typeof(MyCustomLabel)); public MyCustomLabel() { InitializeComponent(); } public string Text { get { if (TextBox.Visibility==Visibility.Visible) { return (string)GetValue(MyCustomLabel.TextProperty); } else { return Label.Content.ToString(); } } set { base.SetValue(MyCustomLabel.TextProperty,value); } } private void EditText(object sender,MouseButtonEventArgs e) { TextBox.Visibility=Visibility.Visible; Label.Visibility=Visibility.Collapsed; Dispatcher.BeginInvoke((ThreadStart)delegate { TextBox.Focus(); TextBox.SelectionStart=TextBox.Text.Length; }); } } So this would be the current flow:
1. Dataobject is initialized, and the MyString property is set
2. The usercontrol which holds the MyCustomLabel is initialized
3. The binding works, and the MyString property shows in the Label
4. The user clicks the Label, the TextBox now shows, and the user changes the value
5. The user clicks a save button, and the content is supposed to be saved, but the MyString property is not updated with the new value
If I debug and access the MyCustomLabel.Text, the property is correctly changed.
I tried implementing the INotifyPropertyChanged in every class, but that did not have any effect.
Hope you can help :)
------- EDIT: ------
Thanks alot for your answers. It pointed me in the right direction.
@Sheridan:
The reason I dont have a single textbox with a readonly state, is I need the functionality and layout of a Label, until the label is clicked and the textbox gets visible. I could probably style my way out of it though.
Anyways, I solved it by binding the Label to the Textbox.Text, and then bind the Textbox.Text to the controls Text property like this:
<DockPanel > <Label Name="Label" MouseLeftButtonDown="EditText" Style="{StaticResource InputLabel}" Content="{Binding ElementName=TextBox, Path=Text}" ></Label> <TextBox Name="TextBox" Style="{StaticResource TextBox}" Visibility="Collapsed" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Text="{Binding Text, RelativeSource={RelativeSource AncestorType=MyNamespace:MyCustomLabel},Mode=TwoWay}" /> </DockPanel> 
MyCustomLabeldefined in XAML?