- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我实现了一个带有图像的简单按钮:
<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/
我有一个针对 .NET Framework 4.0 的应用程序。它构建并运行良好,但我无法让它在 Dotfuscator CE 中编译,而且他们不会支持它,因为它是 CE。我被困住了。 Dotfusc
我的 View 模型中有一个 Icommand,如下所示: public ICommand SalaryCommand { get {
当我实现 ICommand接口(interface),创建以下方法 #region ICommand Members public bool CanExecute(object paramet
下面的代码是我的 WPF 应用程序的 MVVM 类。在我的 MainWindow.xaml.cs 文件 MainWindow() 构造函数中,我已经这样做了。 oneWayAuth = new One
我正在做以下 tutorial ,了解 WPF 中的 MVVM 模式。关于以下看似部分给出的 ICommand 接口(interface)实现,我有些不明白。 在下面的代码中,_canExecute
我定义了一个 ICommand 类 ReadPersons,它使用特定的 where 子句从数据库中读取所有人员对象。 通过按下按钮执行命令,并将 where 子句插入文本框。 问题:如何将文本框的文
我试图理解 ICommand 接口(interface)。我构建了一个带有按钮的应用程序,该按钮使用了一个名为 RelayCommand 的类,该类继承自 ICommand。该类看起来像这样:
在 WPF 中,我有一个名为 Malfunctions 的 ViewModel 类,它有一个 PartMalfunctions 的 ObservableCollection。通常,Observable
我是 C# 新手,这是我的第一个 WPF 项目。我正在关注 this tutorial (使用他们的 RelayCommand 实现并尝试使用 MVVM。我正在实现标准 Windows 计算器的克隆。
我有一个简单的按钮,它在执行时使用一个命令,一切正常,但我想在单击按钮时传递一个文本参数。 我认为我的 XAML 没问题,但我不确定如何编辑我的 RelayCommand 类以接收参数: publi
我有一个 UserControl,里面有一个按钮。此按钮需要将一些项目添加到所述 UC 内的网格中。我知道我可以通过 Click 事件来做到这一点。 这里的问题是我正在使用 MVVM,并且在相应的 V
我已经开始创建一个 wpf mvvm 应用程序。 ViewModel 的一个重要组成部分似乎是一堆 ICommand,它们具有允许 View 与 viewmodel 交互的松散耦合方式。 我的问题是,
我的 wpf-mvvm 应用程序中有一个按钮控件。 我使用 ICommand 属性(在 viewmodel 中定义)将按钮单击事件绑定(bind)到 viewmodel。 我的 ICommand 实现
在 WPF 中,使用 MVVM 设计,我创建了一个屏幕,用于将大量日志加载到 ListView 中。点击一个按钮。返回时,标签会更新以显示返回的日志数量。此过程有时可能需要一段时间。我们的 DA 正在
我正在为 Windows Store 应用程序应用 MVVM 模式(并在此过程中学习它)。 现在我倾向于在 View 和 ViewModel 之间建立 1:1 的对应关系,其中多个 ViewModel
我最近开始在 silverlight 中使用 MVVM 模式,但我不确定我是否正确使用它。 图形用户界面 我目前有一个包含股市板 block 组合框的 MainView。当用户选择一个部门(例如能源)
在我看来,我使用 ItemsControl 来显示几个按钮。 ItemsControl 的 XAML 是: 在我的
我有一个 ICommand实现是独立的。它修改有关实体的信息。同一实体绑定(bind)到 View ,作为 View 的 ViewModel 的属性. 我想要实现的是,在执行命令后,实体(以及因此是
让我们将按钮 Command 属性绑定(bind)到自定义命令。 何时应该实现 ICommand 以及何时从 RoatedCommand 派生?我看到 RoatedCommand 实现了 IComma
基本上,我有一个自定义控件FooControl。 public class FooControl : ItemsControl { //Code } 我需要添加一些事件处理,但与其使用 Rou
我是一名优秀的程序员,十分优秀!