【发布时间】:2011-01-18 16:05:07
【问题描述】:
这是我的场景。我有一个带有标准 int 属性的内部类。
public class Session : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private ushort _Level;
public ushort Level
{
get { return _Level; }
}
private void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
// a timer code here updates the _Level property every 60 seconds
// and calls OnPropertyChange("Level");
}
在我的 UI 窗口中,我有一个绑定到 Session 实例并显示 Level 值的标签。
<TextBlock Name="LevelTextBlock" Text="{Binding SessionInstance.Level, StringFormat='0%'}" />
窗口构造函数有这样的代码:
public MyWindow()
{
SessionInstance = new Session();
this.InitializeComponent();
}
public Session SessionInstance { get; set; }
但是,您可以猜到,当我更新 _Level 的值时,UI 不会更新文本块。在 Session 类中添加 DependancyProperty 是不可接受的。我可以在窗口上执行此操作,但我仍然需要将值检查回 Session 实例的通知程序。 我想知道是否有优雅的方式来做到这一点。除了在窗口中运行另一个计时器来检查和刷新文本块值之外,我想不出任何东西。 有什么想法吗?
【问题讨论】:
标签: wpf binding dependency-properties