设置WPF依赖属性而不触发事件

发布于 2024-07-06 02:19:57 字数 556 浏览 6 评论 0原文

我需要在代码中的控件 (Slider.Value) 上设置依赖属性,而不触发 ValueChanged 事件(因为我设置了值,而不是用户)。 在 WPF 中处理这种情况的最佳方法是什么?

为了澄清起见,我想做的是将 WPF 滑块连接到 WinForms 用户控件。 目前,在我的应用程序中,我有一个 ValueChanged 事件处理程序,它通过方法调用将滑块值传递到 WinForms 控件。 WinForms 控件(实际上是本机 OpenGL 窗口的包装器)需要能够根据其内部计算动态更改滑块。 为此,我使用滑块的抽象 (ISlider),在应用程序中实例化该滑块的 WPF 风格,并通过 WinForms 用户控件上的 .NET 属性将其基础句柄传递给 WinForms 控件。 所有这些目前都在工作,只是当内部逻辑决定滑块需要更改时,它会调用 ISlider::SetPos(),然后更改 WPF 滑块,然后触发滑块上的 ValueChanged 事件,并且处理程序对于该事件,提取滑块的位置并将其传递给最初发起该事件的 WinForms 控件。 ligaz 和 Alan Le 的建议似乎都应该有效,但我不确定我是否会以最好的方式解决这个问题。

I need to set a dependency property on a control (Slider.Value) in my code without it triggering a ValueChanged event (since I set the value, not the user). What is the best way to handle this situation in WPF?

For clarification, what I am trying to do is hook up WPF sliders to a WinForms User Control. Currently in my app I have a ValueChanged event handler that passes the slider value to the WinForms control via a method call. The WinForms control (which is actually a wrapper around a native OpenGL window) needs to be able to dynamically change the slider based on it's internal calculations. To do this I use an abstraction of a slider (ISlider) , I instantiate a WPF-flavor of that slider in my app, and pass a handle to it's base to the WinForms control via a .NET property on the WinForms User Control. All of this is currently working, it's just that when the internal logic decides the slider needs to change, it calls ISlider::SetPos(), which then changes the WPF slider, which then triggers a ValueChanged event on the slider, and the handler for that event extracts the slider's position and passes it in to the WinForms control which originated the event in the first place. The suggestions by ligaz and Alan Le both seem like they should work, but I'm just not sure I'm going about this in the best way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-07-13 02:19:57

您确定您真的想这样做吗? 如果有一段 UI 数据绑定到该属性,并且您允许更改值而不触发 ValueChanged 事件,那么您很快就会发现 UI 不再与数据同步。

对于您的滑块,想象用户将其设置为 75%。 现在,您的代码在后台将其更改为 10%,但抑制更改通知。 它在屏幕上看起来仍然像是 75%(因为没有告知它已更改),但正在运行的代码将其用作 10%。 听起来像是造成混乱的秘诀。

Are you sure you really want to do that? If there's a piece of UI databound to that property and you allow for changing the value without triggering the ValueChanged event you'd quickly end up with your UI no longer synchronized with the data.

In the case of your slider, imagine the user places it at 75%. Now your code changes it to 10% in the background but suppresses the change notification. It still looks on the screen like it's at 75% (since it wasn't told it changed), but it's being used as 10% by the code that's running. Sounds like a recipe for confusion.

一江春梦 2024-07-13 02:19:57

这是一个简单的解决方法/技巧。 添加一个布尔值来跟踪您是否更改了设置,例如“IsChangedByMe”。 当您更改代码中的依赖属性时,请将布尔值设置为 true。 在 ValueChanged 事件中,如果 IsChangedByMe 为 true,则不执行任何操作。

Here's a simply workaround/hack. Add a boolean to keep track whether you changed the setting, let's say "IsChangedByMe". When you change the dependency property in code, set the bool to true. In the ValueChanged event, if IsChangedByMe is true, don't do anything.

葬心 2024-07-13 02:19:57

一种可能的解决方案是从 Slider 派生并重写 OnValueChanged(...)。 当您不想引发事件时,您不应该执行任何操作,否则您应该调用基本实现。

One possible solution is to derive from Slider and override OnValueChanged(...). When you did not want to raise the event you should do nothing, otherwise you should call the base implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文