ValueChanged由用户或在Slider上自动不同 [英] Differ between ValueChanged by user or automatically on Slider

查看:221
本文介绍了ValueChanged由用户或在Slider上自动不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,其中包含一个 Slider sli ,通常会在 ValueChanged 上触发一个方法 - 事件。另外,在某些情况下, sli 的最大和最小值也会更改。现在我意识到,在这些情况下, sli 可能会在超出范围新的最大/最小值。这也将触发 ValueChanged 。另外我有时需要通过编程方式调整 Value ,这当然也会引发ValueChanged。



现在,我只有当用户更改了 Value 时,才希望执行该方法,但是如果它被强制更改,则不会因为超出范围或者在代码中更改。



我尝试使用 DragCompleted -event,但是我希望在用户拖动滑块头时持续触发事件。另一个尝试是在最小/最小更改或代码更新之前,从 ValueChanged 的事件处理程序中简单地删除该方法,然后再添加该方法。在这里,我不知道这是否真的是最干净的,尤其是最快的解决方案,性能明智。



因此,我想问一个人是否可能有更好的解决方案,可能是一些实际上已知的东西,但是,我找不到任何关于这个话题的东西。

解决方案

而不是使用ValueChanged事件可以使用 Binding.SourceUpdated 事件来检测专用的用户输入。 SourceUpdated事件仅由显式用户输入触发。



在滑块上将 NotifyOnSourceUpdated 属性设置为true。 / p>

 < Slider x:Name =sliderValue ={Binding SliderValue,NotifyOnSourceUpdated = True,UpdateSourceTrigger = PropertyChanged,Mode =双向}>< /滑块> 

滑块的父容器可以处理SourceUpdate事件

 < Grid Binding.SourceUpdated =Slider_TargetUpdated> 

private void Slider_TargetUpdated(object sender,DataTransferEventArgs e)
{

}


I have a WPF application that includes a Slider sli, which in general fires a method on the ValueChanged-event. In addition, in certain cases the maximal and minimal values of sli are changed. Now I have realized that in those cases the Value of sli might change in the case that it was out of range of the new max/min values. This will also fire ValueChanged. In addition I sometimes have to adjust Value programmatically, which will of course also fire `ValueChanged.

Now, I only wish to execute the method if the user changed Value, but not if it was forcefully changed, because it went out of range or when I change it in code.

My attempt was using the DragCompleted-event, but I do wish to fire the event continuously while the user drags the slider head. Another attempt was to simply remove the method from the event handler for ValueChanged before a max/min-change or in-code-update and add the method afterwards. Here, I am not sure if that is really the cleanest and especially the fastest solution performance-wise.

Thus, I wanted to ask if someone might have a nicer solution, maybe something that is actually commenly known, however, I couldn't find anything on this topic.

解决方案

Instead of using the ValueChanged event you can use the Binding.SourceUpdated event to detect dedicated user input. The SourceUpdated event is only triggered by explicit user input.

On the slider set the NotifyOnSourceUpdated property to true.

<Slider x:Name="slider" Value="{Binding SliderValue, NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></Slider>

The parent container of the slider can handle the SourceUpdate event

<Grid Binding.SourceUpdated="Slider_TargetUpdated">

private void Slider_TargetUpdated(object sender, DataTransferEventArgs e)
{

}

这篇关于ValueChanged由用户或在Slider上自动不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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