2

我正在从三个相互堆叠的滑块控件中创建一个“范围滑块”。基本思想来自这里,它使用两个滑块。

http://www.thejoyofcode.com/Creating_a_Range_Slider_in_WPF_and_other_cool_tips_and_tricks_for_UserControls_.aspx

我正在添加第三个滑块,其拇指将填充另一个滑块的拇指之间的空间。用户将能够拖动此中心拇指来移动两端并保持两端之间的恒定间距。

XAML 只是三个滑块。让它分层的秘诀在于使用控制模板(这里不重复。你可以在上面的 URL 中找到它)。

<Grid VerticalAlignment="Top">
    <Border BorderThickness="0,1,0,0" BorderBrush="Green" VerticalAlignment="Center" Height="1" 
            Margin="5,0,5,0"/>

    <Slider x:Name="LowerSlider"
            Minimum="{Binding ElementName=root, Path=Minimum}"
            Maximum="{Binding ElementName=root, Path=Maximum}"
            Value="{Binding ElementName=root, Path=LowerValue, Mode=TwoWay}"
            Margin="0,0,0,0"
            Template="{StaticResource simpleSlider}"
            />

    <Slider x:Name="MiddleSlider"
            Minimum="{Binding ElementName=root, Path=Minimum}"
            Maximum="{Binding ElementName=root, Path=Maximum}"
            Value="{Binding ElementName=root, Path=MiddleValue, Mode=TwoWay}"
            Margin="10,0,0,0"
            Template="{StaticResource simpleSlider}"
            >
   </Slider>

    <Slider x:Name="UpperSlider"
            Minimum="{Binding ElementName=root, Path=Minimum}"
            Maximum="{Binding ElementName=root, Path=Maximum}"
            Value="{Binding ElementName=root, Path=UpperValue, Mode=TwoWay}"
            Margin="20,0,0,0"
            Template="{StaticResource simpleSlider}"
            />
</Grid>

当任一外部拇指被拖动时,我需要调整中心拇指的大小以填充两个末端拇指之间的空间。

在后面的代码中,我可以捕捉到拇指的移动,并且可以找到中间滑块控件,但我不知道如何以编程方式获取中间滑块的拇指,以便调整它的大小以填充两个外拇指之间的空间。

private void UpperSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
         Slider slider= (Slider) this.FindName("MiddleSlider");
        // how to find the middleSlider thumb so I can set
        // it's width to fill the space between the outer thumbs
    }

感谢您的任何想法,

米奇

4

2 回答 2

3

This should do it:

var track = (Track)slider.Template.FindName("PART_Track", slider);
var thumb = track.Thumb;
thumb.Width = fittingWidth;

(By the way, I would not do it this way, i would apply a MultiBinding to the two other sliders and a converter which calculates the width from that)

于 2011-05-10T22:06:44.117 回答
1
    private void UpperSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        Slider slider = (Slider) FindName("MiddleSlider");
        Track track = slider.Template.FindName("PART_Track", slider) as Track;
        if (track != null)
        {
            Rectangle thumbRectangle = track.Thumb.Template.FindName("Rect1", track.Thumb) as Rectangle;
            if (thumbRectangle != null)
            {
                thumbRectangle.Width = CalculateWidth();
            }
        }
    }
于 2011-05-11T14:54:04.097 回答