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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
19 deletions
+104
-19
VIZ.Framework.Common/Widgets/NumberBox/NumberBox.cs
+80
-18
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;
...
@@ -14,6 +14,7 @@ using System.Windows.Media;
using
System.Windows.Media.Imaging
;
using
System.Windows.Media.Imaging
;
using
System.Windows.Navigation
;
using
System.Windows.Navigation
;
using
System.Windows.Shapes
;
using
System.Windows.Shapes
;
using
VIZ.Framework.Core
;
namespace
VIZ.Framework.Common
namespace
VIZ.Framework.Common
{
{
...
@@ -109,7 +110,11 @@ 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...
/// Using a DependencyProperty as the backing store for Interval. This enables animation, styling, binding, etc...
/// </summary>
/// </summary>
public
static
readonly
DependencyProperty
IntervalProperty
=
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
#
endregion
...
@@ -143,6 +148,16 @@ namespace VIZ.Framework.Common
...
@@ -143,6 +148,16 @@ namespace VIZ.Framework.Common
private
RepeatButton
PART_Down
;
private
RepeatButton
PART_Down
;
/// <summary>
/// <summary>
/// 之前的文本值
/// </summary>
private
string
old_text
;
/// <summary>
/// 数字正则表达式
/// </summary>
private
Regex
regex
;
/// <summary>
/// 应用模板
/// 应用模板
/// </summary>
/// </summary>
public
override
void
OnApplyTemplate
()
public
override
void
OnApplyTemplate
()
...
@@ -166,34 +181,43 @@ namespace VIZ.Framework.Common
...
@@ -166,34 +181,43 @@ namespace VIZ.Framework.Common
}
}
/// <summary>
/// <summary>
/// 失去焦点
/// </summary>
protected
override
void
OnLostFocus
(
RoutedEventArgs
e
)
{
base
.
OnLostFocus
(
e
);
if
(!
double
.
TryParse
(
this
.
Text
,
out
double
value
))
{
value
=
this
.
MinValue
;
}
value
=
MathHelper
.
Clip
(
this
.
MinValue
,
this
.
MaxValue
,
value
);
value
=
Math
.
Round
(
value
,
MathHelper
.
GetDigitsPrecision
(
value
));
this
.
Text
=
value
.
ToString
();
this
.
Value
=
value
;
}
/// <summary>
/// 文本改变之后
/// 文本改变之后
/// </summary>
/// </summary>
protected
override
void
OnTextChanged
(
TextChangedEventArgs
e
)
protected
override
void
OnTextChanged
(
TextChangedEventArgs
e
)
{
{
base
.
OnTextChanged
(
e
);
base
.
OnTextChanged
(
e
);
if
(
string
.
IsNullOrWhiteSpace
(
this
.
Text
)
)
if
(
this
.
regex
==
null
)
{
{
this
.
Text
=
"0"
;
this
.
ResetRegex
();
return
;
}
}
if
(!
double
.
TryParse
(
this
.
Text
,
out
double
value
))
if
(!
this
.
regex
.
IsMatch
(
this
.
Text
))
{
this
.
Text
=
this
.
Value
.
ToString
();
}
else
if
(
value
<
this
.
MinValue
)
{
this
.
Value
=
this
.
MinValue
;
}
else
if
(
value
>
this
.
MaxValue
)
{
{
this
.
Value
=
this
.
MaxValue
;
this
.
Text
=
this
.
old_text
;
}
}
else
else
{
{
this
.
Value
=
value
;
this
.
old_text
=
this
.
Text
;
}
}
}
}
...
@@ -203,11 +227,12 @@ namespace VIZ.Framework.Common
...
@@ -203,11 +227,12 @@ namespace VIZ.Framework.Common
private
void
PART_Down_Click
(
object
sender
,
RoutedEventArgs
e
)
private
void
PART_Down_Click
(
object
sender
,
RoutedEventArgs
e
)
{
{
double
value
=
this
.
Value
-
this
.
Interval
;
double
value
=
this
.
Value
-
this
.
Interval
;
value
=
Math
.
Round
(
value
,
MathHelper
.
GetDigitsPrecision
(
value
));
if
(
value
<
this
.
MinValue
||
value
>
this
.
MaxValue
)
if
(
value
<
this
.
MinValue
||
value
>
this
.
MaxValue
)
return
;
return
;
this
.
Text
=
value
.
ToString
()
;
this
.
Value
=
value
;
}
}
/// <summary>
/// <summary>
...
@@ -216,12 +241,49 @@ namespace VIZ.Framework.Common
...
@@ -216,12 +241,49 @@ namespace VIZ.Framework.Common
private
void
PART_Up_Click
(
object
sender
,
RoutedEventArgs
e
)
private
void
PART_Up_Click
(
object
sender
,
RoutedEventArgs
e
)
{
{
double
value
=
this
.
Value
+
this
.
Interval
;
double
value
=
this
.
Value
+
this
.
Interval
;
value
=
Math
.
Round
(
value
,
MathHelper
.
GetDigitsPrecision
(
value
));
if
(
value
<
this
.
MinValue
||
value
>
this
.
MaxValue
)
if
(
value
<
this
.
MinValue
||
value
>
this
.
MaxValue
)
return
;
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
...
@@ -35,5 +35,23 @@ namespace VIZ.Framework.Core
return
result
;
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 @@
...
@@ -16,8 +16,13 @@
</UserControl.Resources>
</UserControl.Resources>
<Grid>
<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="0,200,0,0" Click="Button_Click"></Button>
<Button Width="120" Height="30" Margin="400,200,0,0"></Button>
</Grid>
</Grid>
</UserControl>
</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