简体   繁体   中英

WPF Slider Draggable Only

I have a strange requirement to make a slider in a WPF application dragable only, meaning that clicking on the slider track should not move the slider, it should only move when the user clicks and drags the thumb. Is there a way to accomplish this in WPF? I saw a post suggesting you derive a custom control from Slider, but there are only 4 virtual methods, and non seem to be helpful.

This is the slider definition I have now.

<Slider Name="radialSlider" 
        Width="150"  
        VerticalAlignment="Center" 
        TickFrequency=".005" 
        Minimum="-.25" 
        Maximum=".25" 
        IsSnapToTickEnabled="True" 
        ValueChanged="radialSlider_ValueChanged" 
        Thumb.DragCompleted="radialSlider_DragCompleted"  
        Grid.Column="1">
</Slider>

I was able to get what I believe is the expected behavior by intercepting the mouse down event on the radial slider and checking to see if the mouse is over the thumb slider. You forgot to mention you are using the Mahapps UI library . I used a program called Snoop that inspects visual elements in a page to find the name of the MetroThumb control. The MetroThumb element can only be found after the radialSlider has loaded, so you need to call the searching method after the Loaded event of the radialSlider fires.

MetroThumb mt;
loadPage(){
    radialSlider.Loaded += (s, e) => {
        mt = radialSlider.FindChild<MetroThumb>("HorizontalThumb");
    };
    radialSlider.PreviewMouseDown += md;
}

private void md(object sender, RoutedEventArgs e) {
    if (!mt.IsMouseOver) {
        e.Handled = true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM