Commit 4bdac1b6 by liulongfei

数字框优化

parent b1400c78
......@@ -14,6 +14,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VIZ.Framework.Core;
namespace VIZ.Framework.Common
{
......@@ -109,7 +110,11 @@ namespace VIZ.Framework.Common
/// Using a DependencyProperty as the backing store for Interval. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty IntervalProperty =
DependencyProperty.Register("Interval", typeof(double), typeof(NumberBox), new PropertyMetadata(1d));
DependencyProperty.Register("Interval", typeof(double), typeof(NumberBox), new PropertyMetadata(1d, new PropertyChangedCallback((s, e) =>
{
// 重置正则表达式
(s as NumberBox).ResetRegex();
})));
#endregion
......@@ -143,6 +148,16 @@ namespace VIZ.Framework.Common
private RepeatButton PART_Down;
/// <summary>
/// 之前的文本值
/// </summary>
private string old_text;
/// <summary>
/// 数字正则表达式
/// </summary>
private Regex regex;
/// <summary>
/// 应用模板
/// </summary>
public override void OnApplyTemplate()
......@@ -166,34 +181,43 @@ namespace VIZ.Framework.Common
}
/// <summary>
/// 文本改变之后
/// 失去焦点
/// </summary>
protected override void OnTextChanged(TextChangedEventArgs e)
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnTextChanged(e);
base.OnLostFocus(e);
if (string.IsNullOrWhiteSpace(this.Text))
if (!double.TryParse(this.Text, out double value))
{
this.Text = "0";
value = this.MinValue;
}
return;
value = MathHelper.Clip(this.MinValue, this.MaxValue, value);
value = Math.Round(value, MathHelper.GetDigitsPrecision(value));
this.Text = value.ToString();
this.Value = value;
}
if (!double.TryParse(this.Text, out double value))
/// <summary>
/// 文本改变之后
/// </summary>
protected override void OnTextChanged(TextChangedEventArgs e)
{
this.Text = this.Value.ToString();
}
else if (value < this.MinValue)
base.OnTextChanged(e);
if (this.regex == null)
{
this.Value = this.MinValue;
this.ResetRegex();
}
else if (value > this.MaxValue)
if (!this.regex.IsMatch(this.Text))
{
this.Value = this.MaxValue;
this.Text = this.old_text;
}
else
{
this.Value = value;
this.old_text = this.Text;
}
}
......@@ -203,11 +227,12 @@ namespace VIZ.Framework.Common
private void PART_Down_Click(object sender, RoutedEventArgs e)
{
double value = this.Value - this.Interval;
value = Math.Round(value, MathHelper.GetDigitsPrecision(value));
if (value < this.MinValue || value > this.MaxValue)
return;
this.Text = value.ToString();
this.Value = value;
}
/// <summary>
......@@ -216,12 +241,49 @@ namespace VIZ.Framework.Common
private void PART_Up_Click(object sender, RoutedEventArgs e)
{
double value = this.Value + this.Interval;
value = Math.Round(value, MathHelper.GetDigitsPrecision(value));
if (value < this.MinValue || value > this.MaxValue)
return;
this.Text = value.ToString();
this.Value = value;
}
/// <summary>
/// 重置正则表达式
/// </summary>
private void ResetRegex()
{
double precision = MathHelper.GetDigitsPrecision(this.Interval);
StringBuilder sb = new StringBuilder();
sb.Append("^");
if (this.MinValue < 0)
{
if (precision > 0)
{
sb.Append("[-.]*");
}
else
{
sb.Append("[-]*");
}
}
else
{
if (precision > 0)
{
sb.Append("[.]*");
}
}
sb.Append("[0-9]*");
if (precision > 0)
{
sb.Append("[.]*[0-9]{0," + precision + "}");
}
sb.Append("$");
regex = new Regex(sb.ToString());
}
}
}
......@@ -35,5 +35,23 @@ namespace VIZ.Framework.Core
return result;
}
/// <summary>
/// 获数值精度(小数点后几位)
/// </summary>
/// <param name="value">数值</param>
/// <returns>精度</returns>
public static int GetDigitsPrecision(double value)
{
string str = value.ToString();
string[] items = str.Split('.');
if (items.Length != 2)
{
return 0;
}
return items[1].Length;
}
}
}
......@@ -16,8 +16,13 @@
</UserControl.Resources>
<Grid>
<fcommon:NumberBox Height="40" Width="240" Interval="0.1" Value="{Binding Path=Value,Mode=TwoWay}"></fcommon:NumberBox>
<TextBlock Text="{Binding ElementName=nb,Path=Value}"
VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="36" Foreground="Red" Margin="0,0,0,140"></TextBlock>
<fcommon:NumberBox x:Name="nb" Height="40" Width="240" Interval="0.01" Value="{Binding Path=Value,Mode=TwoWay}"></fcommon:NumberBox>
<Button Width="120" Height="30" Margin="0,200,0,0" Click="Button_Click"></Button>
<Button Width="120" Height="30" Margin="400,200,0,0"></Button>
</Grid>
</UserControl>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment