WPF:具有时间格式的滑块精度 [英] WPF: Slider precision with time format
问题描述
我有一个滑块,用于显示选择到格式为00:00:00的文本块中的时间,并将其格式为000000.00作为字符串发送。
I have an slider to display the time selected into a textblock with format 00:00:00 and to send it with format 000000.00 as a string.
<WrapPanel Margin="5" Orientation="Horizontal" x:Name="timeRow" >
<Label TextOptions.TextFormattingMode="Ideal" Width="140" Foreground="#2B3856" FontWeight="Bold" FontSize="16" Margin="3">Time </Label>
<Slider HorizontalAlignment="Left" x:Name="timeSlider_15"
Margin="3"
VerticalAlignment="Top"
Width="250"
ValueChanged="Slider_TimeValueChanged" Minimum="0" Maximum="24" TickFrequency="1" AutoToolTipPlacement="BottomRight" />
<TextBlock TextOptions.TextFormattingMode="Ideal" Width="75" Foreground="#2B3856" FontSize="16" Text="00:00:00" x:Name="TimeText" ></TextBlock>
</WrapPanel>
</WrapPanel>
private void Slider_TimeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var slider = sender as Slider;
TimeSpan Time = TimeSpan.FromHours(slider.Value);
double tiest = slider.Value ;
this.TimeText.Text = Time.ToString("hh\\:mm\\:ss");
///Create MSO sentence
int index = Convert.ToInt32(Regex.Match(slider.Name, @"\d+").Value);
//......
double timeconverted = slider.Value * 10000;
sentenceToSent.SetField(index, Time.ToString("hh\\:mm\\:ss\\.ff").Replace(":", ""));
_myTrucker.Bridge.SendTime(port, host, sentenceToSent.ToString());
}
格式现在不足。我现在的问题是选择的精确度。使用我的代码,当您移动滑块时,文本框不会显示像00:00:01,00:00:02那样的正常值,...而是值增长,如00:06:13,00:12:28, ...我如何解决它保留我的格式?
Formats are now undercontrol. My problem now is the precision of the selection. With my code when you move the slider the textbox doesn't shows values incresing regulari like 00:00:01 , 00:00:02 ,... , Instead values grows like 00:06:13 , 00:12:28 ,...How can I solve it preserving my formats?
推荐答案
将最大值更改为86400。
这是24小时秒。
这将给你1秒的粒度。
请看下面的例子:
从秒到例如的转换12:22:45
将由一个值转换器完成。
TimeConverter.cs
Change Maximum to "86400".
That is 24 hours in seconds.
This will give you a granularity of 1 second.
Please have a look at the example below:
The conversion from seconds to e.g. 12:22:45
will be done by a value converter.
TimeConverter.cs
using System;
using System.Windows.Data;
namespace WpfApplicationSliderTest
{
class TimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string timeString = string.Empty;
int seconds = System.Convert.ToInt32((double)value);
TimeSpan ts = new TimeSpan(0, 0, seconds);
timeString = ts.ToString(@"hh\:mm\:ss");
return timeString;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
MainWindow.xaml:
MainWindow.xaml:
<Window x:Class="WpfApplicationSliderTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplicationSliderTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:TimeConverter x:Key="tc"></local:TimeConverter>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Slider x:Name="slider" Minimum="0" Maximum="86400" SmallChange="1" LargeChange="1" TickFrequency="1" IsSnapToTickEnabled="True" Margin="10"></Slider>
<TextBlock Text="{Binding ElementName=slider,Path=Value,Converter={StaticResource tc}}"></TextBlock>
</StackPanel>
</Grid>
</Window>
拖动滑块不会显示真正的粒度。
步骤似乎更大。
我添加了SmallChange =1。当您使用键盘上的向上/向下箭头键
时,您会看到每次按键都将值更改为1.
我也是添加了LargeChange =1。当您单击滑块按钮的左侧或右侧
时,该值也会增加/减少1.
Dragging the slider does not show the real granularity.
The steps seem to be bigger.
I added SmallChange="1". When you use the arrow up/down keys on your keyboard
you see each key press is changing the value by 1.
I also added LargeChange="1". When you click left or right of the slider button
the value is also increased/decreased by 1.
这篇关于WPF:具有时间格式的滑块精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!