自定义一个按钮控件
按钮控件很简单,我们在项目中有时把样式封装起来,添加依赖属性,也是为了统一。
这里举例,单纯的图标控件怎么设置
1、UserControl界面样式
2、后台设置,我这边只添加了个图片路径和事件委托。其它的自己加吧
public partial class IconButton : UserControl { public IconButton() { InitializeComponent(); } public ImageSource ImagesSource { get { return (ImageSource)GetValue(ImagesSourceProperty); } set { SetValue(ImagesSourceProperty, value); } } public static readonly DependencyProperty ImagesSourceProperty = DependencyProperty.Register("ImagesSource", typeof(ImageSource), typeof(IconButton)); private void IconButton_OnLoaded(object sender, RoutedEventArgs e) { var data = new IconButtonModel() { ImagesSource = ImagesSource }; this.DataContext = data; } public delegate void ClickEventArgs(object sender, RoutedEventArgs e); public event ClickEventArgs Click; private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { if (Click != null) { Click(sender, e); } } } public class IconButtonModel { public ImageSource ImagesSource { get; set; } }