Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
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;
C#
public Mix SelectedDVD
{
  get { return selectedDVD; }
  set
  {
    selectedDVD = value;
    NotifyPropertyChanged("SelectedDVD");
  }
}


On WindowLoaded I do:

MixNameTextBox.DataContext = Data.SelectedDVD;


My XAML looks the following:
C#
<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):
C#
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!
Posted
Updated 22-Jun-12 21:32pm
v2

Hi,
The problem is that your SelectedDVD is not a dependency property. When you bind to the Name property you are not notified of its value changes. So you should either make Name into a Dependency Property or The whole SelectedDVD object into a dependency property.

Here is a working example that I have changed the whole SelectedDVD into a dependency property (Also I have changed its name to Mix) :
C#
public partial class CPTest1 : Window
{
    public static readonly DependencyProperty MixProperty =
        DependencyProperty.Register("Mix", typeof (Mix), typeof (CPTest1), new PropertyMetadata(default(Mix)));

    public Mix Mix
    {
        get { return (Mix) GetValue(MixProperty); }
        set { SetValue(MixProperty, value); }
    }

    public CPTest1()
    {
        InitializeComponent();

        Mix = new Mix("Film 1", 2009);

        DVDNameTextBox.DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Mix = new Mix("Film 2", 2011);
    }

}

public class Mix
{
    public Mix(string name, int year)
    {
        Name = name;
        Year = year;
    }

    public string Name { get; set; }
    public int Year { get; set; }
}





And the XAML part :

XML
<TextBlock Name="DVDNameTextBox" VerticalAlignment="Center" Text="{Binding Path=Mix.Name}" ></TextBlock>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="178,42,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />



For creating dependency properties you can use Intelisense for getting assistance.

Good Luck.
 
Share this answer
 
Comments
NeonMika 24-Jun-12 3:28am    
Thank you for your example on DependencyProperty.
You opend my eyes for something else. I'm going to implement INotifyPropertyChanged on MainWindow and will set "DVDNameTextBox.DataContext = this".
So My DVDNameTextBox gets notified when I change myDVD and I don't need to use DependencyProperties.
Amir Mahfoozi 24-Jun-12 3:38am    
You're welcome.
Yes there are a lot of ways to achieve this :)
Good Luck.
It looks like what you want to do is two-way binding. Here is a Code Project article that should help clear up your questions:

Simple WPF databinding (with some additional WPF goodies)[^]
 
Share this answer
 
Comments
NeonMika 23-Jun-12 3:30am    
No, i don't need two-way-binding, I just want to presentate my value TO the GUI without changes to make.

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 mix selected'}">

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!
The DataContext should be set to MyDvd

For assigning Text="Name"

<textbox datacontext="MyDvd" text="{Binding Name, Mode = TwoWay}">

This works 100%
 
Share this answer
 
Comments
NeonMika 23-Jun-12 3:37am    
It works if my DataContext is never changed.
The problem is, that not a Property is changed, the problem is, that the whole DataContext gets changed and this is not recognized for some reason.

Thanks.

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