【发布时间】:2018-01-16 22:11:37
【问题描述】:
在我的 UWP 应用程序中,我有一个 TextBlock,它应该显示一个(格式化的)日期,它绑定到视图模型中的一个属性:
<TextBlock Style="{StaticResource summaryTextStyleHighlight}" Margin="0,10,0,0"
Text="{x:Bind ViewModel.CurrentDisplayDay, Converter={StaticResource DateFormatConverter}, ConverterParameter=d, Mode=OneWay}"
Name="lblCurrentDate" />
转换器在 XAML 中是这样“配置”的:
<local:DateFormatConverter x:Key="DateFormatConverter" />
而转换器类如下:
public class DateFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
DateTime dt = DateTime.Parse(value.ToString());
if (parameter != null)
{
return dt.ToString((string)parameter, Utils.GetCurrentCultureInfo());
}
return dt.ToString("g", Utils.GetCurrentCultureInfo());
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
ViewModel 有一个用于绑定 DateTime 值的简单属性:
public DateTime CurrentDisplayDay
{
get;
private set;
}
但是当我更新 ViewModel 中的值时,(Main)Page 上的 TextBlock 中的值不会更新。
我尝试将属性移动到页面,但这没有帮助。如果我刷新页面(再次导航到它),则显示更新的值但我不想导航到它,它应该通过绑定显示更新的值。
可能是什么问题?
【问题讨论】:
-
数据绑定并不神奇。必须有某种机制来通知数据的消费者数据已更改。 IE。
INotifyPropertyChanged的实现。请参阅标记的重复项。 -
现在,我知道了——之前没有意识到这一点。
标签: c# data-binding uwp dependency-properties uwp-xaml