4

This question was asked here for thousands times. But really, none of your examples and answers works for me. So let me show you my code.

public class PlayList : INotifyPropertyChanged{ public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string name) { var handler = PropertyChanged; if (handler != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } private string _dl; public string DriveLetter { get { return _dl; } set { if (value != _dl) { _dl = value; OnPropertyChanged("DriveLetter"); } } } } public partial class MainWindow : Window { public PlayList playlist = new PlayList(); private void Window_Loaded(object sender, RoutedEventArgs e) { Binding bind = new Binding("DriveLetter"); bind.Source = this.playlist; bind.Mode = BindingMode.TwoWay; bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; textBox1.SetBinding(TextBlock.TextProperty, bind); this.playlist.DriveLetter = "A"; } } 

Ofcourse WPF ignores this binding (nothing changes when I type in textbox, and nothing changes when I change playlist.DriveLetter property.

Debugger says, that PropertyChanged handler is not null

{Method = {Void OnPropertyChanged(System.Object, System.ComponentModel.PropertyChangedEventArgs)}} 

So, any ideas what I am doing wrong. (I do not belive that WPF is wrong)?

Thanks in advance!

4
  • Is there any reason why you are building the binding in code instead of XAML? And have you tried using tools like Snoop for debugging the bindings, or have you tried setting breakpoints in your property to see if any code execution is taking place there? Commented Dec 7, 2011 at 12:49
  • Yes, there is quite good reason. I want to use playlist object later in code. Commented Dec 7, 2011 at 12:51
  • I wouldn't say that's a good reason to do it this way. Commented Dec 7, 2011 at 14:04
  • You can declare your Playlist object in code while setting it as the DataContext of the TextBox, or simply provide a property for it that the XAML can refer to for it's source. Commented Dec 7, 2011 at 14:54

3 Answers 3

6

Change

textBox1.SetBinding(TextBlock.TextProperty, bind); 

to

textBox1.SetBinding(TextBox.TextProperty, bind); 
Sign up to request clarification or add additional context in comments.

Comments

3

you don't have to do it this way, even if you want to use your playlist later on. Just use a Property in your Window like:

public PlayList PlayList { get; private set; } 

and Bind your TextBox like this:

<TextBox Text="{Binding Path=PlayList.DriveLetter}"/> 

you also have to set the DataContext of the Window, I think:

DataContext = this; 

or you set the Data Context to your PlayList:

DataContext = PlayList; 

so the Binding looks like this:

<TextBox Text="{Binding Path=DriveLetter}"/> 

Comments

2

change

 textBox1.SetBinding(TextBlock.TextProperty, bind); 

to

 textBox1.SetBinding(TextBox.TextProperty, bind); 

You are binding TextBlock's text property rather than TexBox's text property

1 Comment

I am really sorry. Really sorry for my blindness (too fast typing + InteliSense). I'm going to Castorama to buy some rope. 2 Days wasted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.