DependencyObject.InvalidateProperty不工作 [英] DependencyObject.InvalidateProperty not working
问题描述
根据在documentation通过MSDN ...
Based on the documentation via MSDN...
您也可以使用InvalidateProperty来
有约束力的力量重新评估
针对不能够将数据源
实施建议
INotifyPropertyChanged的通知
机制...
You can also use InvalidateProperty to force re-evaluation of a binding against a data source that is not able to implement the recommended INotifyPropertyChanged notification mechanism...
...的code以下应该工作,但它没有。
...the code below should work, yet it doesn't.
public partial class Window1 : Window
{
private Payload _payload = new Payload();
public Window1()
{
InitializeComponent();
this.DataContext = _payload;
}
private void Invalidate(object sender, RoutedEventArgs e)
{
_payload.Timestamp = DateTime.Now.Add(TimeSpan.FromHours(1)).ToLongTimeString();
Button b = sender as Button;
b.InvalidateProperty(Button.ContentProperty);
}
}
public class Payload
{
private String _payload = DateTime.Now.ToLongTimeString();
public String Timestamp
{
get
{
return _payload;
}
set
{
_payload = value;
}
}
}
<Grid>
<Button Click="Invalidate"
Width="100"
Height="50"
Content="{Binding Path=Timestamp}"/>
</Grid>
任何想法是什么原因造成这种现象?
Any idea what is causing this behavior?
推荐答案
正如你所提到的,它应该工作,但没有。但是有一个简单的解决方法:
As you mentioned, it ought to work but doesn't. But there is a simple workaround:
// Doesn't work:
//b.InvalidateProperty(Button.ContentProperty);
// Works:
BindingOperations.GetBindingExpression(b, Button.ContentProperty).UpdateTarget();
我调试到参考源和所有 InvalidateProperty
确实在你的情况是会导致缓存值从 BindingEx $ P重新读$ pssion
到按钮
内容
属性。随口说说,我不知道什么时候这甚至是必要的,但它不是有用得到 BindingEx pression
来重新阅读的原始属性。
I debugged into the reference source and all InvalidateProperty
does in your situation is cause a cached value to be re-read from the BindingExpression
into the Button
Content
property. Offhand, I don't know when this would even be necessary but it's not useful to get the BindingExpression
to re-read the raw property.
由于解决方法是方便,一般情况下,保证只有进一步努力是提交与微软的bug报告。
Since the workaround is convenient and general, the only further effort warranted is filing a bug report with Microsoft.
这篇关于DependencyObject.InvalidateProperty不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!