WPF:控件绑定 - 从 C# 代码触发 IValueConverter
在我的 XAML 代码中,我已将控件的 Height 属性绑定到其父控件的 ActualHeight。 因为我必须计算与我使用的原始值和 IValueConverter 的偏移量。
Height="{Binding ElementName=MainCanvas, Path=ActualHeight, Converter={StaticResource adjustVerteilung} , ConverterParameter= 12}"
到目前为止效果很好。
在发生某些事件后,ValueConverter 内的公式确实会发生变化并生成不同的输出。 但只有在我调整窗口大小后,控件才会刷新和对齐。 有没有办法告诉控件(从 C# 代码)刷新自身并从 ValueConverter 获取新值?
In my XAML Code I have bound the Height property of an control to the ActualHeight for it's parent control. Because I have to calculate an offset from the original value I use and IValueConverter.
Height="{Binding ElementName=MainCanvas, Path=ActualHeight, Converter={StaticResource adjustVerteilung} , ConverterParameter= 12}"
This works fine so far.
After some event the formula inside the ValueConverter does change and generates a different output. But the controls are only refreshed and aligned after I resize my window. It there any way to tell the control (from the C# code) to refresh itself and get a new value from the ValueConverter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将 UpdateSourceTrigger=PropertyChanged 添加到绑定中,则它应该在实际高度更改后立即重新绑定到该值,而不是在调整窗口大小并且 MainCanvas 刷新绑定之后。
If you add UpdateSourceTrigger=PropertyChanged to your binding, it should rebind to the value as soon as Actual height changes, instead of after the window is resized and the bindings are refreshed by MainCanvas.
我认为即使 ActualHeight 没有改变你也想改变值。 据我所知,ValueConverter 功能不是旨在执行类似的操作。
你应该尝试一些不同的东西。 如果您实际上希望在“某些事件”发生时更新观察到的高度,也许您可以尝试构建一个自定义面板,其“某些属性”以某种方式绑定到您的事件。 然后,当“某些事件”发生时,您可以更改面板内控件的观察高度。
I think you want to change value even if ActualHeight does not change. AFAIK the ValueConverter feature is not designed to do something like that.
You should try something different. If it is actually the observed height that you wish to update when your "some event" happens, maybe you can try building a custom panel, whose "some property" is somehow bound to your event. Then, when "some event" happens, you can change the observed height of the control(s) that lie inside the panel.