wpf - 如何数据绑定(bind) WPF Slider 的 Thumb 的 DragCompleted 事件

标签 wpf xaml mvvm data-binding slider

我想将 DragCompleted 事件绑定(bind)到我的 ViewModel 的命令之一。我使用 Blend 尝试了以下方法,但它不起作用:

<Slider x:Name="slider" HorizontalAlignment="Left" Margin="41,147,0,0" VerticalAlignment="Top" Width="412">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Thumb.DragCompleted">
            <i:InvokeCommandAction Command="{Binding DragCompletedCommand}"></i:InvokeCommandAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Slider>

但这行不通。当我使用事件的正常绑定(bind)来编码时,它起作用了:

<Slider x:Name="slider" Thumb.DragCompleted="slider_DragCompleted"  HorizontalAlignment="Left" Margin="41,147,0,0" VerticalAlignment="Top" Width="412"></Slider>

我尝试搜索但奇怪的是找不到答案。

最佳答案

您可以为此编写一个附加属性,如下所示:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace MyTestApplication
{
    internal class SliderExtension
    {
        public static readonly DependencyProperty DragCompletedCommandProperty = DependencyProperty.RegisterAttached(
            "DragCompletedCommand",
            typeof(ICommand),
            typeof(SliderExtension),
            new PropertyMetadata(default(ICommand), OnDragCompletedCommandChanged));

        private static void OnDragCompletedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Slider slider = d as Slider;
            if (slider == null)
            {
                return;
            }

            if (e.NewValue is ICommand)
            {
                slider.Loaded += SliderOnLoaded;
            }
        }

        private static void SliderOnLoaded(object sender, RoutedEventArgs e)
        {
            Slider slider = sender as Slider;
            if (slider == null)
            {
                return;
            }
            slider.Loaded -= SliderOnLoaded;

            Track track = slider.Template.FindName("PART_Track", slider) as Track;
            if (track == null)
            {
                return;
            }
            track.Thumb.DragCompleted += (dragCompletedSender, dragCompletedArgs) =>
            {
                ICommand command = GetDragCompletedCommand(slider);
                command.Execute(null);
            };
        }

        public static void SetDragCompletedCommand(DependencyObject element, ICommand value)
        {
            element.SetValue(DragCompletedCommandProperty, value);
        }

        public static ICommand GetDragCompletedCommand(DependencyObject element)
        {
            return (ICommand)element.GetValue(DragCompletedCommandProperty);
        }
    }
}

然后您的 Slider-Definition 看起来像:

<Slider x:Name="slider" HorizontalAlignment="Left" Margin="41,147,0,0" VerticalAlignment="Top" Width="412" 
        extensions:SliderExtension.DragCompletedCommand="{Binding SlideCompletedCommand}"/>

extensions 是附加属性所在的命名空间。

在您的 ViewModel 中,您有一个名为 SlideCompletedCommand 的 ICommand-Property,它看起来像:

private ICommand slideCompletedCommand;
public ICommand SlideCompletedCommand
{
    get { return slideCompletedCommand ?? (slideCompletedCommand = new RelayCommand(p => SlideCompleted())); }
}

private void SlideCompleted()
{
    // Your slide-completed-code here
}

关于wpf - 如何数据绑定(bind) WPF Slider 的 Thumb 的 DragCompleted 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40848343/

相关文章:

c# - Xceed WPF 工具包中的 Zoombox 无法正常工作

c# - XAML 中可重用、可重新着色的图标使用什么结构?

C# WPF 数据网格 |只有一行没有内容

c# - 如何在 dataGrid Cell 工具提示上设置多重绑定(bind)?

c# - 箭头键绑定(bind)不触发

c++ - '系统' : a namespace with this name does not exist

wpf - 默认 RenderTransform 转换器语法

c# - 透明背景png重叠路径

mvvm - 绑定(bind)到 mvvm 中的常量值

c# - 使用 MVVM 将错误的 WPF 文本框滚动到 View 中