开发者

Binding WPF TextBlock to custom object in application

开发者 https://www.devze.com 2023-04-06 18:31 出处:网络
In MainWindow.xaml I have ... <TextBlock x:Name=\"tbCpu\" Text=\"{Binding Path=activeTower.cpuTotal}\" />

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

0

精彩评论

暂无评论...
验证码 换一张
取 消