【问题标题】:Range slider metro style app范围滑块 Metro 风格应用
【发布时间】:2012-04-06 00:36:30
【问题描述】:

我正在尝试为 Metro 风格应用程序制作一个范围滑块,因为它不可用。我正在尝试利用现有的 silverlight 范围滑块,但效果不佳。我稍微更改了代码,但现在它只显示滑块,我无法移动拇指。

这里是 xaml 代码:

<UserControl x:Class="Mecoms_Mobile_App.RangeSlider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Top">

    <Grid.Resources>
        <ControlTemplate x:Key="buttonTemplate" TargetType="RepeatButton">
            <!-- just empty-->
            <Grid />
        </ControlTemplate>
        <ControlTemplate x:Key="sliderTemplate" TargetType="Slider">
            <Grid x:Name="HorizontalTemplate" Background="{TemplateBinding Background}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <RepeatButton Template="{StaticResource buttonTemplate}" IsTabStop="False" IsEnabled="False" x:Name="HorizontalTrackLargeChangeDecreaseRepeatButton" Grid.Column="0"/>
                <Thumb IsTabStop="True" Height="18" x:Name="HorizontalThumb" Width="11" Grid.Column="1">
                    <Thumb.Template>
                        <ControlTemplate TargetType="Thumb">
                            <Rectangle Fill="Red"
                                       Stroke="Black"
                                       StrokeThickness="1" />
                        </ControlTemplate>
                    </Thumb.Template>
                </Thumb>
                <RepeatButton Template="{StaticResource buttonTemplate}" IsTabStop="False" IsEnabled="False" x:Name="HorizontalTrackLargeChangeIncreaseRepeatButton" Grid.Column="2"/>
            </Grid>
        </ControlTemplate>

    </Grid.Resources>

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

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

    <Slider x:Name="UpperSlider"
            Minimum="{Binding Minimum}"
            Maximum="{Binding Maximum}"
            Value="{Binding UpperValue, Mode=TwoWay}"
            Margin="10,0,0,0"
            Template="{StaticResource sliderTemplate}"
            />
</Grid>
</UserControl>

背后的代码:

public sealed partial class RangeSlider : UserControl
{
    public RangeSlider()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(RangeSlider_Loaded);

        LayoutRoot.DataContext = this;
    }

    void RangeSlider_Loaded(object sender, RoutedEventArgs e)
    {
        LowerSlider.ValueChanged += LowerSlider_ValueChanged;
        UpperSlider.ValueChanged += UpperSlider_ValueChanged;
    }

    void UpperSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        LowerSlider.Value = Math.Min(UpperSlider.Value, LowerSlider.Value);
    }

    void LowerSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        UpperSlider.Value = Math.Max(UpperSlider.Value, LowerSlider.Value);
    }

    public double Minimum
    {
        get { return (double)GetValue(MinimumProperty); }
        set { SetValue(MinimumProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Minimum.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MinimumProperty =
        DependencyProperty.Register("Minimum", typeof(double), typeof(RangeSlider), new PropertyMetadata(0d));

    public double Maximum
    {
        get { return (double)GetValue(MaximumProperty); }
        set { SetValue(MaximumProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Maximum.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(double), typeof(RangeSlider), new PropertyMetadata(1d));

    public double LowerValue
    {
        get { return (double)GetValue(LowerValueProperty); }
        set { SetValue(LowerValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LowerValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LowerValueProperty =
        DependencyProperty.Register("LowerValue", typeof(double), typeof(RangeSlider), new PropertyMetadata(0d));

    public double UpperValue
    {
        get { return (double)GetValue(UpperValueProperty); }
        set { SetValue(UpperValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for UpperValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UpperValueProperty =
        DependencyProperty.Register("UpperValue", typeof(double), typeof(RangeSlider), new PropertyMetadata(0d));
}

这段代码有问题吗?或者有其他方法可以实现吗?

【问题讨论】:

  • 或许你应该更具体一些,加个图片,这样就清楚了!
  • 这里是范围滑块截图的链接:gyazo.com/08a5760cafd2a91fbe94be93f048b33d.png?1332749326
  • 没人知道吗?或者其他解决方案的建议?
  • 您找到范围滑块的解决方案了吗?我也需要一个。

标签: c# xaml microsoft-metro .net-4.5


【解决方案1】:

迟到总比没有好,但我试图让相同的代码工作。添加

<Rectangle x:Name="HorizontalDecreaseRect" Grid.Row="1"/>

在 ControlTemplate 中的 Thumb 元素之前。这将允许拇指移动。

【讨论】:

  • 这就是解决方案,我遇到了同样的问题,尝试了上面的代码并发现了错误,这个矩形就像一个魅力,但你能解释一下为什么吗?我不明白 Rectangle 发生了什么变化:D
猜你喜欢
  • 2015-03-06
  • 1970-01-01
  • 2018-11-21
  • 2011-02-10
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多