fishsticks
fishsticks

Reputation: 753

Binding WPF TextBlock to custom object in application

In MainWindow.xaml I have

...
<TextBlock x:Name="tbCpu" Text="{Binding Path=activeTower.cpuTotal}" />
...

and in MainWindow.xaml.cs I have

public partial class MainWindow : Window
{
    Tower activeTower
    public MainWindow()
    {
        activeTower = Tower();
        activeTower.cpuTotal = 500;
        tbCpu.DataContext = this;
    }
}

The code compiles and runs fine, without any errors. However, tbCpu stays empty. Tower is a custom class that has a property cpuTotal that returns a double, but I have tried other properties in the same class that return a string and it still doesn't work. What am I doing wrong here?

Upvotes: 1

Views: 3019

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

activeTower needs to be a public property for the binding to work:

public Tower activeTower{get;set;}

If you want changes of activeTower to be reflected in the View then you need to implement the INotifyPropertyChanged interface in your class

Upvotes: 3

Related Questions