WPF slider IsSelectionRangeEnabled TickFrequency

<Window x:Class="WpfApp426.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp426"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:TwoDigitsConverter x:Key="twoDigitsConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Slider x:Name="slider" Grid.Row="0"
                Foreground="Blue"
                Minimum="0" Maximum="100" 
                IsSelectionRangeEnabled="True"
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                TickPlacement="BottomRight" 
                TickFrequency="1"/>
        <TextBlock Grid.Row="1" Text="{Binding Path=Value, StringFormat={}{0:0}, ElementName=slider}" 
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 FontSize="30"/>
        <TextBlock Grid.Row="2" Text="{Binding Path=Value, StringFormat=1 Digit:{0:0.0}, ElementName=slider}" 
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="30"/>
        <TextBlock Grid.Row="3" Text="{Binding Path=Value, StringFormat=2 Digits:{0:0.00}, ElementName=slider}" 
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 FontSize="30"/>
        <TextBlock Grid.Row="4" Text="{Binding Path=Value, StringFormat=3 Digits:{0:0.000}, ElementName=slider}" 
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="30"/>
        <TextBlock Grid.Row="5" Text="{Binding Path=Value,Converter={StaticResource twoDigitsConverter},ElementName=slider}" 
               HorizontalAlignment="Center"
               VerticalAlignment="Top"
               FontSize="30"/>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp426
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); 
        }
    }

    public class TwoDigitsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value?.ToString()=="0")
            {
                return 0;
            }
            double d;
            if(double.TryParse(value?.ToString(),out d))
            {
                return Math.Round(d, 2);
            }
            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2024-09-30 18:25  FredGrit  阅读(5)  评论(0编辑  收藏  举报