IconButton.xaml.cs
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/****************************************************************
* 作 者 :姜 彦
* 项目名称 :RCS.WinClient.Controls.View
* 控件名称 :IconButton
* 命名空间 :RCS.WinClient.Controls.View
* CLR 版本 :4.0.30319.42000
* 创建时间 :2017/8/4 9:10:14
* 当前版本 :1.0.0.1
* My Email:771078740@qq.com
* 描述说明:
*
* 修改历史:
*
****************************************************************
* Copyright @ JiangYan 2018 All rights reserved
****************************************************************/
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace RCS.WinClient.Controls.View
{
[System.ComponentModel.DesignTimeVisible(false)]//在工具箱中 隐藏该窗体 20170804 姜彦
public partial class IconButton : UserControl
{
public IconButton()
{
InitializeComponent();
this.button.Click += delegate
{
RoutedEventArgs newEvent = new RoutedEventArgs(IconButton.ClickEvent, this);
this.RaiseEvent(newEvent);
};
}
#region 图标
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon",
typeof(string),
typeof(IconButton),
new PropertyMetadata(string.Empty, OnIconChanged));
public string Icon
{
set { SetValue(IconProperty, value); }
get { return (string)GetValue(IconProperty); }
}
private static void OnIconChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
IconButton btn = obj as IconButton;
if (btn == null)
{
return;
}
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri((string)args.NewValue, UriKind.RelativeOrAbsolute);
bitmap.CacheOption = BitmapCacheOption.OnLoad; // 禁用缓存
bitmap.EndInit();
bitmap.Freeze();
btn.icon.Source = bitmap;
}
#endregion
#region 命令
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command",
typeof(ICommand),
typeof(IconButton),
new PropertyMetadata(null, OnSelectCommandChanged));
public ICommand Command
{
set { SetValue(CommandProperty, value); }
get { return (ICommand)GetValue(CommandProperty); }
}
private static void OnSelectCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
IconButton btn = obj as IconButton;
if (btn == null)
{
return;
}
btn.button.Command = (ICommand)args.NewValue;
}
#endregion
#region 点击事件
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(IconButton));
public event RoutedEventHandler Click
{
add
{
base.AddHandler(ClickEvent, value);
}
remove
{
base.RemoveHandler(ClickEvent, value);
}
}
#endregion
}
}