Hello community!
Learning WPF isn't that easy :P
I have the following problem:
I have a Textbound which DataContext is bound to a "DVD"-instance in backcode:
DvdNameTextBox.DataContext = myDvd;
But if I change my DVD like the following:
myDvd = new Dvd("From outer space", 1978);
my Binding doesn't get updated.
Now i tried to put myDvd into a class called MainWindowData, which i made INotifyPropertyChanged and tried to set the DataContext to
myData.myDvd
...Still no update.
Now i found out if i set the DataContext to
myData
and the Binding to
Text="myDvd.Name"
that the text gets updated because my PropertyChangedEventHandler isn't null anymore (I think because I used myData as DataContext)
Would have been there an more easy way to achieve data binding? I just want to get my DVDs name and have to change to DVD instance in backcode.
Thanks for your help and sorry for the long question :)
--------------------------------
--------------------------------
--------------------------------
IMPROVE QUESTION:
I have in MainWindowData:
private Mix selectedDVD = null;
public Mix SelectedDVD
{
get { return selectedDVD; }
set
{
selectedDVD = value;
NotifyPropertyChanged("SelectedDVD");
}
}
On WindowLoaded I do:
MixNameTextBox.DataContext = Data.SelectedDVD;
My XAML looks the following:
<TextBlock Name="DVDNameTextBox" VerticalAlignment="Center" Text="{Binding Name, Converter={StaticResource ReplaceNullStringConverter},ConverterParameter='No DVD selected'}" ></TextBlock>
And on ListBox.SelectionChanged (the ListBox to select the DVD):
Data.SelectedDVD = (DVD)e.AddedItems[0];
And on this line there is the problem: The program doesn't mind that the source was changed.
It doesn't change anything to implement INotifyPropertyChange on MyWindowData and/or on DVD.
Why doesn't my Textbox realise that it has a new DataSource?
Thanks!