.Net 4.0 中 .Net 4.5 的绑定延迟属性
如何从 .Net 4.5 实现 Delay 属性(描述 此处)关于 .Net 4.0 中的绑定?
我知道我无法从 BindingBase 继承,因为 ProvideValue 是密封的。
我可以实现 MarkupExtension 但这意味着我现在必须重写 BindingExtension 中的所有属性还有其他方法吗?
How can I implement Delay property from .Net 4.5 (described here) on binding in .Net 4.0?
I know I cannot inherit from BindingBase as ProvideValue is sealed.
I could implement MarkupExtension but it means I now have to rewrite all properties from BindingExtension is there any other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后我决定使用组合来实现 DelayedBinding 作为 MarkupExtension。
我遇到的唯一问题是,如果来自
IProvideValueTarget
的TargetProperty
为 null,则 DataTemplatesProvideValue
应该返回此值。t the end I've decided to implement DelayedBinding as MarkupExtension using composition.
The only problem I had was with DataTemplates
ProvideValue
should return this ifTargetProperty
fromIProvideValueTarget
is null.我将创建一个
AttachedProperty
来指定延迟的时间量。当绑定值更改时,AttachedProperty
将启动(或重置)计时器,并在达到指定时间量时手动更新绑定源。您可以使用以下内容来更新源绑定:
编辑
我今天正在修复一些旧代码中的错误,并注意到它使用附加行为实现了延迟属性更改通知。我想到了这个问题,所以按照我在代码中评论的链接进行操作,发现自己遇到了我不久前在 SO 上发布的关于 延迟绑定。最重要的答案是我当前实现的答案,它是一些附加属性,可在 X 毫秒过去后更新绑定的源。
I would create an
AttachedProperty
that specifies the amount of time to Delay. TheAttachedProperty
would start (or reset) a timer when the bound value changes, and would manually update the bound source when the specified amount of time gets reached.You can use the following to update the source binding:
Edit
I was fixing a bug in some old code today and noticed it implemented a delayed property change notification using an Attached Behavior. I thought of this question, so followed the link that I had commented in the code, and found myself at a question I had posted a while ago on SO about delaying a binding. The top answer is the one I have implemented currently, which is some attached properties that updates the source of a binding after X milliseconds have passed.
直接移植是不可能的,但我们可以使用 MultiBinding “模拟”它
请注意,这是一个非常紧密耦合的解决方案,如果在页面上使用许多此类绑定,则可能无法很好地执行...
两个 <强>必须有 ...
ArrayList
中的延迟(以毫秒为单位)作为转换器参数。测试 XAML 如下...
在此示例中,
TextBlock
在 2 秒延迟后呈现在下面的TextBox
中键入的内容。TextBox.Text
是主要数据源。DelayHelper
是多转换器,其工作原理如下所示...因此,此代码利用了
TextBlock
) 及其依赖属性 (TextBlock.TextProperty
) 本身是多重绑定的。ConveterParameter
。但转换器参数本身可以是一个引用对象,它在整个绑定活动期间保持其引用,例如ArrayList
。DispatcherTimer
必须在第一个Tick
之后停止。因此,我们使用temp
变量是非常必要的。请告诉我这是否有帮助...
Straightaway porting is not possible but can we "simulate" this using
MultiBinding
Mind you that this is very tightly coupled solution and may not perform well if many of such bindings are used on a page...
Two must haves ...
ArrayList
as a converter parameter.The Test XAML is as below...
In this example a
TextBlock
renders of what is typed inTextBox
below after a 2 seconds delay. TheTextBox.Text
is primary source of data.DelayHelper
is multi converter that works as shown below...So this code makes use of the facts that
TextBlock
) and its dependency property (TextBlock.TextProperty
) that itself is multi-bound.ConveterParameter
. But the converter parameter itself can be a reference object that maintains its reference throughout the binding is active e.g.ArrayList
.DispatcherTimer
has to stop after its firstTick
. Hence we use of thetemp
variable is very essential.Let me know if this helps...