static
KnobButton
(
)
DefaultStyleKeyProperty
.
OverrideMetadata
(
typeof
(
KnobButton
)
,
new
FrameworkPropertyMetadata
(
typeof
(
KnobButton
)
)
)
;
public
KnobButton
(
)
SetCurrentValue
(
WidthProperty
,
150d
)
;
SetCurrentValue
(
HeightProperty
,
150d
)
;
SetCurrentValue
(
MaximumProperty
,
100d
)
;
MouseDown
+=
Path_MouseDown
;
MouseMove
+=
Path_MouseMove
;
MouseWheel
+=
Path_MouseWheel
;
MouseLeftButtonUp
+=
KnobButton_MouseLeftButtonUp
;
Update
(
)
;
#
region
绘制
private
void
InitTick
(
)
for
(
int
i
=
0
;
i
<
11
;
i
++
)
Line
line
=
new
Line
(
)
;
line
.
X1
=
0
;
line
.
Y1
=
0
;
line
.
X2
=
0
;
line
.
Y2
=
12
;
line
.
Stroke
=
Brushes
.
Gray
;
line
.
StrokeThickness
=
2
;
line
.
HorizontalAlignment
=
HorizontalAlignment
.
Center
;
line
.
RenderTransformOrigin
=
new
Point
(
0.5
,
0.5
)
;
line
.
RenderTransform
=
new
RotateTransform
(
)
{
Angle
=
-
140
+
i
*
28
}
;
bdGrid
.
Children
.
Add
(
line
)
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
var
start
=
-
140
+
28
*
i
+
2.8
;
for
(
int
j
=
0
;
j
<
9
;
j
++
)
Line
line
=
new
Line
(
)
;
line
.
X1
=
0
;
line
.
Y1
=
0
;
line
.
X2
=
0
;
line
.
Y2
=
6
;
line
.
Stroke
=
Brushes
.
Gray
;
line
.
StrokeThickness
=
1
;
line
.
HorizontalAlignment
=
HorizontalAlignment
.
Center
;
line
.
RenderTransformOrigin
=
new
Point
(
0.5
,
0.5
)
;
line
.
RenderTransform
=
new
RotateTransform
(
)
{
Angle
=
start
+
j
*
2.8
}
;
bdGrid
.
Children
.
Add
(
line
)
;
#
endregion
protected
override
void
OnValueChanged
(
double
oldValue
,
double
newValue
)
base
.
OnValueChanged
(
oldValue
,
newValue
)
;
Update
(
)
;
protected
override
void
OnMaximumChanged
(
double
oldMaximum
,
double
newMaximum
)
base
.
OnMaximumChanged
(
oldMaximum
,
newMaximum
)
;
Update
(
)
;
protected
override
void
OnMinimumChanged
(
double
oldMinimum
,
double
newMinimum
)
base
.
OnMinimumChanged
(
oldMinimum
,
newMinimum
)
;
Update
(
)
;
public
string
ValueChangedExecute
get
{
return
(
string
)
GetValue
(
ValueChangedExecuteProperty
)
;
}
set
{
SetValue
(
ValueChangedExecuteProperty
,
value
)
;
}
public
static
readonly
DependencyProperty
ValueChangedExecuteProperty
=
DependencyProperty
.
Register
(
"ValueChangedExecute"
,
typeof
(
string
)
,
typeof
(
KnobButton
)
,
new
PropertyMetadata
(
string
.
Empty
)
)
;
public
int
Step
get
{
return
(
int
)
GetValue
(
StepProperty
)
;
}
set
{
SetValue
(
StepProperty
,
value
)
;
}
public
static
readonly
DependencyProperty
StepProperty
=
DependencyProperty
.
Register
(
"Step"
,
typeof
(
int
)
,
typeof
(
KnobButton
)
,
new
PropertyMetadata
(
1
)
)
;
RotateTransform
rotatevalue
;
Grid
bdGrid
;
public
override
void
OnApplyTemplate
(
)
base
.
OnApplyTemplate
(
)
;
rotatevalue
=
GetTemplateChild
(
"rotatevalue"
)
as
RotateTransform
;
bdGrid
=
GetTemplateChild
(
"bdGrid"
)
as
Grid
;
Update
(
)
;
InitTick
(
)
;
private
void
Update
(
)
if
(
rotatevalue
==
null
)
return
;
double
perangle
=
280
/
(
Maximum
-
Minimum
)
;
double
angle
=
(
perangle
*
(
Value
-
Minimum
)
)
+
40
;
DoubleAnimation
da
=
new
DoubleAnimation
(
)
;
da
.
Duration
=
new
Duration
(
TimeSpan
.
FromMilliseconds
(
350
)
)
;
da
.
EasingFunction
=
new
CubicEase
(
)
{
EasingMode
=
EasingMode
.
EaseOut
}
;
da
.
To
=
angle
;
rotatevalue
.
Angle
=
angle
;
rotatevalue
.
BeginAnimation
(
RotateTransform
.
AngleProperty
,
da
)
;
Point
lastpoint
;
private
void
Path_MouseMove
(
object
sender
,
MouseEventArgs
e
)
if
(
e
.
LeftButton
==
MouseButtonState
.
Released
)
return
;
CaptureMouse
(
)
;
Point
point
=
e
.
GetPosition
(
this
)
;
double
xmove
=
point
.
X
-
lastpoint
.
X
;
double
ymove
=
point
.
Y
-
lastpoint
.
Y
;
double
changeValue
=
(
xmove
+
ymove
)
/
10
*
Step
;
if
(
(
changeValue
+
Value
)
>
Maximum
)
if
(
Value
<
Maximum
)
Value
=
Maximum
;
return
;
if
(
(
changeValue
+
Value
)
<
Minimum
)
if
(
Value
>
Minimum
)
Value
=
Minimum
;
return
;
Value
=
changeValue
+
Value
;
lastpoint
=
point
;
private
void
Path_MouseDown
(
object
sender
,
MouseButtonEventArgs
e
)
if
(
e
.
LeftButton
==
MouseButtonState
.
Released
)
return
;
lastpoint
=
e
.
GetPosition
(
this
)
;
private
void
KnobButton_MouseLeftButtonUp
(
object
sender
,
MouseButtonEventArgs
e
)
ReleaseMouseCapture
(
)
;
private
void
Path_MouseWheel
(
object
sender
,
MouseWheelEventArgs
e
)
double
changeValue
=
(
e
.
Delta
/
120
)
*
Step
;
if
(
(
changeValue
+
Value
)
>
Maximum
)
if
(
Value
<
Maximum
)
Value
=
Maximum
;
return
;
if
(
(
changeValue
+
Value
)
<
Minimum
)
if
(
Value
>
Minimum
)
Value
=
Minimum
;
return
;
Value
=
Value
+
changeValue
;
Update
(
)
;
Xaml代码:
<Style TargetType="{x:Type ctrl:KnobButton}">
<Setter Property="Background" Value="#0068F4"/>
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ctrl:KnobButton">
<Grid x:Name="bdGrid" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}">
<Grid Margin="16" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<RotateTransform x:Name="rotatevalue" Angle="00"/>
</Grid.RenderTransform>
<Ellipse Margin="4" Fill="#FFF6F6F6" Stroke="{StaticResource ControlBorderBrush}" >
<Ellipse.Effect>
<DropShadowEffect ShadowDepth="2" BlurRadius="8" Direction="-90" Color="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Background.(SolidColorBrush.Color)}"/>
</Ellipse.Effect>
</Ellipse>
<Ellipse Margin="12" Fill="{TemplateBinding Background}" Width="8" Height="8" VerticalAlignment="Bottom">
</Ellipse>
</Grid>
<TextBlock Text="{Binding Value,RelativeSource={RelativeSource Mode=TemplatedParent}, StringFormat={}{0:F2}}"
VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
WPF 原创旋钮 KnobButtonC#代码:public class KnobButton : Slider, IExecutable { static KnobButton() { DefaultStyleKeyProperty.OverrideMetadata(typeof(KnobButton), new FrameworkPropertyMetadata(typeof(KnobButton))); } .
界面:c#窗口中包含一个panel控件,panel上面有一个ElementHost控件,另外还有两个按钮。
代码:两个按钮分别对应导入图片,和定时器旋转图片(定时器函数中采用Region_Update函数将方形的elementhost编程椭圆)
WPF Button中主要设定了一个参数-旋转中心(0.5,0.5)。
WPF 中的Image自带旋转函数。
代码简洁,适合新手看。
采用vs2010编写。
but.Background = Brushes.LightSteelBlue;//按钮控件的背景颜色
but.Width = but.Height = 100;//按钮控件的宽高大小
xz.Children.Add(but);//将按钮控件添加到Canvas画布中
//设置位置在窗口中居中
Canvas.SetLeft(but, this.Width / 2 - but.Width / 2);
Canvas.SetTop(but, thi
<Window x:Class="PMA.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/wi...
static CustomDetails()
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomDetails), new FrameworkPropertyMetadata(typeof(CustomDetails)));
public.
static TextBoxEx()
DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
public TextBoxEx()
用户控件的目标是提供增补控件模板的设计表面,提供一种定义控件的快速方法,代价是失去了将来的灵活性。如果喜欢用户控件的功能,但需要修改使其可视化外观,使用这种方法就有问题了。例如,设想希望使用相同的颜色拾取器,但希望使用不同的“皮肤”,将其更好地融合到已有的应用程序窗口中。可以通过样式来改变用户控件的某些方面,但该控件的一些部分是在内部锁定,并硬编码到标记中。例如,无法将预览矩形移动到滑动条的左边。
解决方法是创建无外观控件——继承自控件基类,但没有设计表面的控件。相反,这个控件将其标记放到默认模板.
WPF 修改控件默认的ScrollView样式
WPF中控件自带的ScrollView样式不太好看,可以用下面方式进行修改,本例以ListBox和TreeView控件为例,因为新的ScrollView需要鼠标停留在上面才会显示,可以看下图右边的TreeView中的ScrollView样式:
首先看一下程序结构:
新建一个ScrollView文件夹,在里面添加一个自定义控件ZScrollView,
ZScrollView代码如下:
namespace WpfApp11.ScrollView
1. 创建自定义控件类,并在 XAML 中定义控件的模板和样式。
2. 在需要添加控件的代码中,直接实例化自定义控件,并设置其属性和事件。
3. 将控件添加到需要添加的容器中,比如一个 StackPanel 或 Grid。
4. 要删除控件,可以直接从容器中移除控件。
下面是一个动态添加和删除自定义控件的示例:
```csharp
// 创建自定义控件类
public class MyControl : Control
public MyControl()
// 在构造函数中设置控件的模板和样式
DefaultStyleKey = typeof(MyControl);
// 在 XAML 中定义控件的模板和样式
<Style TargetType="local:MyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<!-- 控件模板 -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
// 在代码中动态添加控件
var myControl = new MyControl();
myControl.Width = 100;
myControl.Height = 50;
myControl.Content = "My Control";
myControl.Click += MyControl_Click;
myContainer.Children.Add(myControl);
// 在代码中动态删除控件
myContainer.Children.Remove(myControl);
其中,`myContainer` 可以是任何容器控件,比如一个 StackPanel 或 Grid。