我在启动时启用了双向绑定的滑块。但是当我移动它们时,移动并不顺畅。因此,我决定在调用 Thumbs.DragStarted 事件后将绑定模式从双向更改为单向源,但这会导致立即将值从 1 更改为 0。
我的问题是:如何防止滑块在新绑定期间更改其值?
我的代码如下。
private void SliderCameraZ_OnDragStarted(object sender, DragStartedEventArgs e)
{
try
{
BindingExpression bindingExpression = ((Slider) sender).GetBindingExpression(Slider.ValueProperty);
BindingOperations.ClearBinding((Slider) sender, Slider.ValueProperty);
Binding binding = new Binding();
binding.Path = bindingExpression.ParentBinding.Path;
binding.Mode = BindingMode.OneWayToSource;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
((Slider)sender).SetBinding(Slider.ValueProperty, binding);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message + exception.StackTrace + exception.TargetSite);
}
}