EventToCommand没有开火

时间:2013-06-25 20:40:48

标签: wpf mvvm mvvm-light

我正在尝试使用eventToCommand来确定用户何时将鼠标悬停在滑块控件上。然而它永远不会被解雇。这包含在数据模板

<DataTemplate x:Key="RunEventsTemplate">
    <di:DIGroupBox Grid.Row="2" Header="Real-Time Modifications" DataContext="{Binding DataContext.ScenarioHelper.EventPlayingService,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <di:DIGroupBox Header="Audio" DataContext="{Binding CurrentAudioEvent}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <di:DILabel Content="Filename: " />
                    <di:DIComboBox Grid.Column="1" />
                    <di:DICheckBox Grid.Row="1">Loop Audio File?</di:DICheckBox>
                    <di:DIImageToggleButton Grid.Row="1" Grid.Column="1" Height="23" Width="23" HorizontalAlignment="Left" Image="PlayIcon_White"></di:DIImageToggleButton>
                </Grid>
            </di:DIGroupBox>
            <di:DIGroupBox Grid.Row="1" Header="Odors">
                <ItemsControl ItemsSource="{Binding VehicleOdors}" ItemTemplate="{StaticResource ScentTemplate}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel></WrapPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </di:DIGroupBox>
            <di:DIGroupBox Grid.Row="2" Header="Lighting" DataContext="{Binding CurrentLightingEvent}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width=".3*" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <di:DILabel Content="Color: " />
                    <di:DILabel Grid.Row="1" Content="Strobe: " />
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <di:DIRadioButton DIRadioButtonStyle="ToggleButton" IsChecked="{Binding CurrentDMXLightColor,Converter={StaticResource enumConverter},ConverterParameter={x:Null}}" Content="Off" Margin="0,0,5,0" Height="23" Width="50"></di:DIRadioButton>
                        <di:DIRadioButton DIRadioButtonStyle="ToggleButton" IsChecked="{Binding CurrentDMXLightColor,Converter={StaticResource enumConverter},ConverterParameter=White}" Content="White" Margin="0,0,5,0" Height="23" Width="50" />
                        <di:DIRadioButton DIRadioButtonStyle="ToggleButton" IsChecked="{Binding CurrentDMXLightColor,Converter={StaticResource enumConverter},ConverterParameter=Red}" Content="Red" Margin="0,0,5,0" Height="23" Width="50"/>
                        <di:DIRadioButton DIRadioButtonStyle="ToggleButton" IsChecked="{Binding CurrentDMXLightColor,Converter={StaticResource enumConverter},ConverterParameter=Blue}" Content="Blue"  Height="23" Width="50"/>
                    </StackPanel>
                    <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
                        <TextBlock Text="Always On" Foreground="White" VerticalAlignment="Center" TextWrapping="Wrap" MaxWidth="50" Margin="0,0,5,0" />
                        <di:DISlider Minimum="0" Maximum="100" Value="{Binding StrobeRate}" >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseUp">
                                      <cmd:EventToCommand Command="{Binding UserChangedSliderValueCommand}"></cmd:EventToCommand>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </di:DISlider>
                        <TextBlock Text="Fast Strobe" Foreground="White" VerticalAlignment="Center" TextWrapping="Wrap" MaxWidth="50" />
                    </StackPanel>
                </Grid>
            </di:DIGroupBox>
            <di:DIGroupBox Header="Smoke" Grid.Row="3" DataContext="{Binding CurrentSmokeEvent}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="60" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <di:DIToggleButton Content="Off" Width="50" Height="23">
                       </di:DIToggleButton>
                    <di:DILabel Grid.Column="1" Content="Intensity:" Margin="10,0,0,0" />
                    <di:DISlider Grid.Column="2" VerticalAlignment="Center" Value="{Binding Intensity}" Minimum="0" Maximum="100" >

                    </di:DISlider>
                </Grid>
            </di:DIGroupBox>
        </Grid>
    </di:DIGroupBox>
</DataTemplate>

命令看起来像这样

 private RelayCommand userChangedSliderValueCommand;
    public RelayCommand UserChangedSliderValueCommand
    {
        get 
        {
            return userChangedSliderValueCommand = (userChangedSliderValueCommand??new RelayCommand(() =>
                {
                    LightingEvent newLe = new LightingEvent();
                    newLe.DMXLightColor = CurrentDMXLightColor;
                    newLe.StrobeRate = StrobeRate;
                    UserAddNewEvent(newLe);
                }
                )); 
        }
    }

我设置了一个断点,它永远不会被击中。我正在使用.net 4.5

1 个答案:

答案 0 :(得分:0)

不确定您的DISlider是否可以作为标准WPF Slider执行此操作,但您可以尝试。首先,您应该获得Slider的Thumb对象,请参阅Get the Thumb of a Slider

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

然后你会听Thumb的DragCompleted事件,这是我项目中的一个例子:

    #region Manage Deferred Scrolling

    void VerticalScrollBarLoaded(object sender, RoutedEventArgs e)
    {
        VerticalScrollBar.Track.Thumb.DragStarted -= ThumbDragStarted;
        VerticalScrollBar.Track.Thumb.DragCompleted -= ThumbDragCompleted;
        VerticalScrollBar.Track.Thumb.DragStarted += ThumbDragStarted;
        VerticalScrollBar.Track.Thumb.DragCompleted += ThumbDragCompleted;
    }

    void ThumbDragCompleted(object sender, DragCompletedEventArgs e)
    {
        IsDeferredScrolling = false;
        if (CancelSelectionCrossingPage)
            SelectionSettings.SelectedCells.Clear();
    }

    void ThumbDragStarted(object sender, DragStartedEventArgs e)
    {
        IsDeferredScrolling = true;
    }

    #endregion

此外,您可以考虑绑定滑块值。我认为这应该是更好的方式。如果困难,您可以继承您的控件并添加依赖项属性来实现它。