如何数据绑定WPF滑块';拇指';s DragCompleted事件

如何数据绑定WPF滑块';拇指';s DragCompleted事件,wpf,xaml,mvvm,data-binding,slider,Wpf,Xaml,Mvvm,Data Binding,Slider,我想将DragCompleted事件绑定到我的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事件绑定到我的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>

但这不起作用。当我使用事件到代码隐藏的正常绑定时,它可以工作:

<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);
        }
    }
}
private ICommand slideCompletedCommand;
public ICommand SlideCompletedCommand
{
    get { return slideCompletedCommand ?? (slideCompletedCommand = new RelayCommand(p => SlideCompleted())); }
}

private void SlideCompleted()
{
    // Your slide-completed-code here
}
您的滑块定义如下所示:

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

我可能错了,但我想我以前也遇到过这种情况。因为Thumb.DragCompleted实际上不是Slider的一部分(它连接到Slider),所以此交互触发器不会响应它。但是,如果您编辑滑块模板的副本,您可以将交互触发器直接粘贴到拇指上,这将起作用。谢谢。它起作用了。但由于拇指是slider的一部分,他们应该提供一种通过XAML访问它的方法。