gpt4 book ai didi

c# - 在 WPF 用户控件中附加 ICommand

转载 作者:太空狗 更新时间:2023-10-29 20:06:39 25 4
gpt4 key购买 nike

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

    <Button Command="{Binding ButtonCommand, ElementName=ImageButtonControl}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ButtonImage, ElementName=ImageButtonControl}"/>
<TextBlock Text="{Binding ButtonText, ElementName=ImageButtonControl}" Margin="2,0,0,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="{Binding AttachContextCommand}" ButtonImage="{StaticResource AssociateImage}" ButtonText="Associer"/>

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

我什至尝试过简单地扩展 Button 类并绑定(bind)一个 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));

}

<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,0,0" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

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

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

注意:

我敢肯定通过“Template”属性并使用 ControlTemplate 和 TemplateBindings 会更干净,但这意味着要在您的内容周围重新创建边框和其他内容,所以如果您只想定义一个默认的“内容”,我认为我的例子就是要走的路。

关于c# - 在 WPF 用户控件中附加 ICommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/643689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com