Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VIZ.Framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王永辉
VIZ.Framework
Commits
4bdac1b6
Commit
4bdac1b6
authored
Jul 08, 2022
by
liulongfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数字框优化
parent
b1400c78
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
18 deletions
+103
-18
VIZ.Framework.Common/Widgets/NumberBox/NumberBox.cs
+79
-17
VIZ.Framework.Core/Math/MathHelper.cs
+18
-0
VIZ.Framework.WpfTest/NumberBoxTest/NumberBoxTest.xaml
+6
-1
No files found.
VIZ.Framework.Common/Widgets/NumberBox/NumberBox.cs
View file @
4bdac1b6
...
...
@@ -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
On
TextChanged
(
TextChang
edEventArgs
e
)
protected
override
void
On
LostFocus
(
Rout
edEventArgs
e
)
{
base
.
On
TextChanged
(
e
);
base
.
On
LostFocus
(
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
());
}
}
}
VIZ.Framework.Core/Math/MathHelper.cs
View file @
4bdac1b6
...
...
@@ -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
;
}
}
}
VIZ.Framework.WpfTest/NumberBoxTest/NumberBoxTest.xaml
View file @
4bdac1b6
...
...
@@ -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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment