4

I'm trying to trigger Slider Thumb.DragStarted event by using MVVMLight EventToCommand but it is not working. The same thing is working perfectly for Slider Event ValueChanged.

Below is my code:

<Slider
    Width="150"
    AutoToolTipPlacement="BottomRight"
    AutoToolTipPrecision="2"
    IsSnapToTickEnabled="True"
    Maximum="{Binding SilderMaxValue}"
    Minimum="0"
    Value="{Binding SliderValue}">                                        
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="ValueChanged">
                <cmd:EventToCommand
                    Command="{Binding SliderValueChangedCommand}"
                    PassEventArgsToCommand="True" />
             </i:EventTrigger>
        <i:EventTrigger EventName="Thumb.DragStarted">
                <cmd:EventToCommand
                    Command="{Binding SliderDragStartedCommand}"
                    PassEventArgsToCommand="True" />
        </i:EventTrigger>                                  
</Slider>

Thanks..

4

2 に答える 2

4

同様のことをしようとしているときにあなたの投稿を見ました(Thumb.DragCompletedではありますが)。いずれにせよ、添付プロパティを使用しました。誰にとっても役立つ場合に備えて、ソリューションを投稿します。


SliderDragBehavoirs.cs:

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

namespace WpfApplication1
{
    public static class SliderDragBehaviors
    {
        public static readonly DependencyProperty DragCompletedCommandProperty =
            DependencyProperty.RegisterAttached("DragCompletedCommand", typeof(ICommand), typeof(SliderDragBehaviors),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(DragCompleted)));

        private static void DragCompleted(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var slider = (Slider)d;
            var thumb = GetThumbFromSlider(slider);

            thumb.DragCompleted += thumb_DragCompleted;
        }

        private static void thumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)sender;
            element.Dispatcher.Invoke(() =>
                {
                    var command = GetDragCompletedCommand(element);
                    var slider = FindParentControl<Slider>(element) as Slider;
                    command.Execute(slider.Value);
                });
        }

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

        public static ICommand GetDragCompletedCommand(FrameworkElement element)
        {
            var slider = FindParentControl<Slider>(element);
            return (ICommand)slider.GetValue(DragCompletedCommandProperty);
        }

        private static Thumb GetThumbFromSlider(Slider slider)
        {
            var track = slider.Template.FindName("PART_Track", slider) as Track;
            return track == null ? null : track.Thumb;
        }

        private static DependencyObject FindParentControl<T>(DependencyObject control)
        {
            var parent = VisualTreeHelper.GetParent(control);
            while (parent != null && !(parent is T))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            return parent;
        }
    }
}

ここで注目すべき点がいくつかあります。コマンドは Slider に接続されていますが、イベントは Thumb で発生するため、一方から他方を取得するには、ビジュアル ツリーを上下に検索できる必要があります。


XAML の例:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:behaviors="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="grid">
        <Slider behaviors:SliderDragBehaviors.DragCompletedCommand="{Binding Path=DragCompletedCommand}"/>
    </Grid>
</Window>

それが役に立つことを願っています:)

于 2013-02-15T10:54:34.930 に答える