与MVVMLight一起出现“Thumb.DragStarted”事件

时间:2013-01-15 04:38:18

标签: wpf xaml slider mvvm-light

我正在尝试使用MVVMLight Slider触发Thumb.DragStarted EventToCommand事件,但它无效。滑块事件ValueChanged也是如此。

以下是我的代码:

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

谢谢..

2 个答案:

答案 0 :(得分: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>

希望有一些用处:)

答案 1 :(得分:1)

我遇到了Tom Allen的代码问题,因为当我想用命令绑定它时滑块模板不可用。基本上,我需要做的就是等待滑块控件加载并再试一次。 以下是我需要进行的更改才能使其正常工作:

    private static void DragCompleted(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //the Template of the slider is not available now
        //we have to wait for the slider to load completely in order to do this
        var slider = (Slider)d;
        slider.Loaded += slider_Loaded;
    }

    static void slider_Loaded(object sender, RoutedEventArgs e)
    {
        var slider = (Slider)sender;
        var thumb = GetThumbFromSlider(slider);
        thumb.DragCompleted += thumb_DragCompleted;
    }

希望它有所帮助! 此致