c# – 在WPF UserControl中附加ICommand

前端之家收集整理的这篇文章主要介绍了c# – 在WPF UserControl中附加ICommand前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我实现了一个带有图像的简单按钮:
<Button Command="{Binding ButtonCommand,ElementName=ImageButtonControl}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ButtonImage,ElementName=ImageButtonControl}"/>
            <TextBlock Text="{Binding ButtonText,ElementName=ImageButtonControl}" Margin="2,0"/>
        </StackPanel>
    </Button>

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

public partial class ImageButton : UserControl
{
    /// <summary>
    /// The dependency property that gets or sets the source of the image to render.
    /// </summary>
    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();
    }

    /// <summary>
    /// Gets or sets the button command.
    /// </summary>
    public ICommand ButtonCommand
    {
        get { return (ICommand)GetValue(ImageButton.ButtonCommandProperty); }
        set { SetValue(ImageButton.ButtonCommandProperty,value); }
    }

    /// <summary>
    /// Gets or sets the button image.
    /// </summary>
    public ImageSource ButtonImage
    {
        get { return (ImageSource)GetValue(ImageButton.ImageSourceProperty); }
        set { SetValue(ImageButton.ImageSourceProperty,value); }
    }

    /// <summary>
    /// Gets or sets the button text.
    /// </summary>
    public string ButtonText
    {
        get { return (string)GetValue(ImageButton.TextProperty); }
        set { SetValue(ImageButton.TextProperty,value); }
    }
}

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

< UC:ImageButton的Grid.Row = “1” Grid.Column = “0” ButtonCommand = “{结合AttachContextCommand}” 按钮画面= “{StaticResource的AssociateImage}” ButtonText = “Associer”/>

还有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(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),new UIPropertyMetadata(null));

    }

<Style TargetType="{x:Type Button}"
               x:Key="ImageButton">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Path=(local:ImageButton.Image),RelativeSource={RelativeSource AncestorType={x:Type Button}}}" />
                            <TextBlock Text="{Binding Path=(local:ImageButton.Caption),RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
                                       Margin="2,0" />
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>   
        </Style>

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

<Button Style="{DynamicResource ImageButton}"
                        local:ImageButton.Caption="Foo" 
                        local:ImageButton.Image="..." />

注意:

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

猜你在找的C#相关文章