XAML 如何绑定WPF Slider的Thumb的DragCompleted事件

sr4lhrrt  于 2023-10-14  发布在  其他
关注(0)|答案(2)|浏览(176)

我想将DragCompleted事件绑定到ViewModel的一个命令。我尝试了以下使用混合,但它不工作:

<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>

我试着寻找,但奇怪的是找不到答案。

zy1mlcev

zy1mlcev1#

你可以为它写一个附加的属性,它看起来像这样:

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
}
siotufzp

siotufzp2#

我最终使用后面的代码来访问ViewModel。
在xaml中:

<Slider Thumb.DragCompleted="OnThumbDragCompleted"/>

在代码隐藏中(考虑到视图的DataContext是ViewModel):

private void OnThumbDragCompleted(object sender, DragCompletedEventArgs e)
{
    ((ViewModel)this.DataContext).DragCompleted();
}

在ViewModel中:

public void DragCompleted()
{
    // event processing
}

希望这对你有帮助!

相关问题