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 开发者_StackOverflow社区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?
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
精彩评论