设置WPF依赖属性而不触发事件 [英] Setting WPF dependency property without triggering events

查看:465
本文介绍了设置WPF依赖属性而不触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

推荐答案

这是一个简单的解决方法/黑客。添加一个布尔值来跟踪是否更改了设置,假设IsChangedByMe。当您更改代码中的依赖属性时,将bool设置为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.

这篇关于设置WPF依赖属性而不触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆