行业知识
WPF制作带图标和文字的按钮模板(通过附加属性实现)
2026-11-29 09:07:43
来源:互联网转载
1.���面模板代码部分
()2.在工程中添加类,用于编辑附加属性,主要是为了设置图标地址。下面为附加属性代码
//通过附加属性来设置图标的路径 public static class ButtonExtensions { public static readonly DependencyProperty IconPathProperty = DependencyProperty.RegisterAttached( "IconPath", typeof(string), typeof(ButtonExtensions), new PropertyMetadata(null)); public static string GetIconPath(DependencyObject obj) { return (string)obj.GetValue(IconPathProperty); } public static void SetIconPath(DependencyObject obj, string value) { obj.SetValue(IconPathProperty, value); } } 3.界面按钮代码实现代码,如下
()4.如果想要从后台代码修改按钮上的图片,则代码如下
ButtonExtensions.SetIconPath(myButton, "pack://application:,,,/Resources/NewImage.png" );
上一篇:多个服务器如何履行?