在WPF UserControl中附加ICommand

我实现了一个带有图像的简单按钮:

 

如您所见,我公开了一个ButtonCommand属性,以便能够将ICommand附加到此UserControl:

 public partial class ImageButton : UserControl { ///  /// The dependency property that gets or sets the source of the image to render. ///  public static DependencyProperty ImageSourceProperty = DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(ImageButton)); public static DependencyProperty TextProperty = DependencyProperty.Register("ButtonText", typeof(string), typeof(ImageButton)); public static DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(ImageButton)); public ImageButton() { this.DataContext = this; InitializeComponent(); } ///  /// Gets or sets the button command. ///  public ICommand ButtonCommand { get { return (ICommand)GetValue(ImageButton.ButtonCommandProperty); } set { SetValue(ImageButton.ButtonCommandProperty, value); } } ///  /// Gets or sets the button image. ///  public ImageSource ButtonImage { get { return (ImageSource)GetValue(ImageButton.ImageSourceProperty); } set { SetValue(ImageButton.ImageSourceProperty, value); } } ///  /// Gets or sets the button text. ///  public string ButtonText { get { return (string)GetValue(ImageButton.TextProperty); } set { SetValue(ImageButton.TextProperty, value); } } } 

然后,当我声明我的按钮时,它给出了:

还有badaboom,当我点击我的ImageButton时,什么都不会发生。 当我用一个简单的按钮替换ImageButton时,会调用ICommand。

我甚至试图简单地扩展Button类并绑定一个ICommand,但是再一次,它没有用……

帮助赞赏!

谢谢。

您可以使用样式和几个附加属性以更清晰的方式实现此目的。

附加的属性将存储您的特定信息。 该样式将使用这些属性并构建您想要的外观。

该元素仍然是一个按钮,所以命令和其他一切都可以工作。

  public class ImageButton { public static ImageSource GetImage(DependencyObject obj) { return (ImageSource)obj.GetValue(ImageProperty); } public static void SetImage(DependencyObject obj, ImageSource value) { obj.SetValue(ImageProperty, value); } public static readonly DependencyProperty ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null)); public static String GetCaption(DependencyObject obj) { return (String)obj.GetValue(CaptionProperty); } public static void SetCaption(DependencyObject obj, String value) { obj.SetValue(CaptionProperty, value); } public static readonly DependencyProperty CaptionProperty = DependencyProperty.RegisterAttached("Caption", typeof(String), typeof(ImageButton), new UIPropertyMetadata(null)); }  

然后,您可以使用它来声明按钮:

   

注意:

我很确定通过“模板”属性并使用ControlTemplate和TemplateBindings会更清晰,但这意味着重新创建内容周围的边框和其他内容,因此如果您只想定义默认值我认为,“内容”,我的例子将是要走的路。

如果您想要为按钮添加的唯一function是在其上有图像,那么我认为您正在从错误的方向接近这个。 WPF非常棒,因为UI控件看起来很简洁。 这意味着Control只是function的定义+一些模板来定义它的外观。 这意味着可以随时换出模板以更改外观。 此外,几乎任何内容都可以放在几乎任何控件内

例如,要在您的xaml中定义一个具有您所需要的外观的按钮,请执行以下操作:

  ...  ...  

请记住,使用WPF时,每次要更改某些内容的外观时,都不必定义新的CustomControl或UserControl。 您唯一需要CustomControl的时间是,您希望向现有Control添加function或创建任何其他Control中不存在的function。

编辑由于评论:

如果你不想每次都不要定义按钮的内容,另一个选择是只有一个poco(普通的旧CLR对象)类来定义你感兴趣的一切(我会像你一样写我的例子)为工具栏做这个,因为它对我有意义:

 public class ToolBarItem : INotifyPropertyChanged { public string Text { get ... set ... } public ICommand Command { get ... set ... } public ImageSource Image { get ... set ... } } 

那里有一个定义的数据模板(App.xaml,Window.Resources等):

    

然后在你的xaml中使用这样的人:

  ...      ...  

我只是不知道你尝试这样做的方式是你可以做到的最WPF方式。

编辑根据第二条评论更新对不起,我忘了包含ContentControl。 现在我记得那个,我意识到,这比你手动指定内容的原始文件要简单得多。 我会发布一个新的答案来帮助您解决原始问题。

要重新回答原始问题:

我认为你想要做的是创建一个名为ImageButton的新CustomControl。 然后将其更改为从Button而不是Control扩展。 您不需要Command属性,因为Button已经有一个。 您只需要添加一个Image属性,并且可以从按钮重用Content属性而不是Text属性。

创建CustomControl后,它将在Generic.xaml中添加一个条目,用于ImageButton的默认样式。 在Setter for Template属性中,您可以将ControlTemplate更改为:

       

然后,当你想要使用它时:

  ...  Associer  ...  

内置的WPF按钮包含激活附加命令以响应被单击的代码。 您的“ImageButton”派生自UserControl,因此您不会获得该行为。 获得所需内容的最短路径可能是您的ImageButton类实际派生自WPF Button类。 为此,请更改ImageButton的标记

  ...  

  

然后将ImageButton的基类从UserControl更改为Button

在一切正常之前,您可能需要进行一些其他的微小更改。