0

I am trying to use an eventToCommand to determine when the user mouses up on a slider control. However it is never getting fired. This is contained within a data template

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

The command looks like this

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

I set a break point and it never gets hit. I am using .net 4.5

4

1 回答 1

0

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

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

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

于 2013-06-26T16:00:01.720 回答