<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(); } } }
标签:Windows,object,System,TickFrequency,slider,value,using,WPF,public From: https://www.cnblogs.com/Fred1987/p/18442306