TextBlock不会更新 [英] TextBlock Won't Update
问题描述
我正在尝试更新的 TextBlock
( caloriesAvailableTextBlock
)。 Button
( eatCaloriesButton
)应该减少 TextBlock
被绑定到100.但是, TextBlock
不会更新。它只是在2000年。任何想法我失踪了?
I have a TextBlock
(caloriesAvailableTextBlock
) which I am trying to update. The Button
(eatCaloriesButton
) is supposed to reduce the number which the TextBlock
is bound to by 100. However, the TextBlock
will not update. It just remains at 2000. Any ideas what I am missing?
我的xaml在HubPage.xaml:
My xaml in HubPage.xaml:
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="Calories Available:" FontSize="24"/>
<TextBlock x:Name="caloriesAvailableTextBlock" Loaded="caloriesAvailableTextBlock_Loaded" TextWrapping="Wrap" Text="{Binding}" FontSize="36"/>
<Button x:Name="eatCaloriesButton" Content="Eat 100 Calories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontSize="18" Click="eatCaloriesButton_Click" FontFamily="Global User Interface"/>
</StackPanel>
我的代码在HubPage.xaml.cs中:
My code behind in HubPage.xaml.cs:
public CalorieTracker CalorieTracker { get; set; }
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
CalorieTracker = new CalorieTracker();
CalorieTracker.CaloriesAvailable = 2000;
}
private void eatCaloriesButton_Click(object sender, RoutedEventArgs e)
{
CalorieTracker.CaloriesAvailable -= 100;
}
private void caloriesAvailableTextBlock_Loaded(object sender, RoutedEventArgs e)
{
((TextBlock)sender).DataContext = CalorieTracker.CaloriesAvailable;
}
我的 CalorieTracker.cs
class,其中包含我正在更新的数字:
My CalorieTracker.cs
class which holds the number which I am updating:
public class CalorieTracker : INotifyPropertyChanged
{
private int caloriesAvailable;
public int CaloriesAvailable
{
get { return caloriesAvailable; }
set { caloriesAvailable = value;
NotifyPropertyChanged("CaloriesAvailable");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的理解是, code> CalorieTracker.CaloriesAvailable 已更改,它将让该变量的所有出现都知道,但这不是发生了什么。任何想法为什么不?或者我只是离开了基地?
My understanding is that whenever CalorieTracker.CaloriesAvailable
is changed, it will let all occurences of that variable know, but that is not what is happening. Any idea why not? Or am I just way off base?
推荐答案
这里的问题似乎是如何设置绑定。
The problem here appears to be how you set up your binding.
将整个DataContext 设置为您的文本框的int。这是不是你想做什么。为了更新变量更改,那么很多东西都必须是不同的(对于初学者,运行时必须在 DataContextChanged
而不是 PropertyChanged
)。
You set the whole DataContext to that int for your textblock. This is not what you want to do. For that to update on variable change, well, a lot of stuff would have to be different (for starters the runtime would have to listen on DataContextChanged
instead of PropertyChanged
).
相反,将页面的 DataContext
设置为您的视图模型,然后绑定到一个属性:
Instead, set the DataContext
for the page to your view model, then bind to a property:
<TextBlock x:Name="caloriesAvailableTextBlock" TextWrapping="Wrap" Text="{Binding CaloriesAvailable}" FontSize="36"/>
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
DataContext = CalorieTracker = new CalorieTracker();
CalorieTracker.CaloriesAvailable = 2000;
}
现在您的 NotifyPropertyChanged
实际上会做你想要的,你的UI会更新。这是更适合MVVM模式。
Now your NotifyPropertyChanged
will actually do what you expected, and your UI will update. This is a much better fit for the MVVM pattern anyways.
这篇关于TextBlock不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!