继承的WPF自定义控件不继承父命令

菲贝尔

我创建了一个IconButton以在WPF / XMAML中使用。它应该能够在顶部显示Icon MDL2 Assets字体,在底部显示文本。它应该具有默认的WPF工具栏按钮的外观。我决定创建一个自定义控件,该控件继承自默认WPF按钮。

因此,我创建了自定义控件,并为Text和某种程度上神秘的MDL2IconCode添加了Dependency Properties:

public class IconButton : Button
{
  public static readonly DependencyProperty TextProperty;
  public static readonly DependencyProperty MDL2IconCodeProperty;

  public string Text
  {
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
  }


  public string MDL2IconCode
  {
    get { return (string)GetValue(MDL2IconCodeProperty); }
    set { SetValue(MDL2IconCodeProperty, value); }
  }


  static IconButton()
  {
    DefaultStyleKeyProperty.OverrideMetadata(typeof(IconButton),
                                             new FrameworkPropertyMetadata(typeof(IconButton)));

    TextProperty = DependencyProperty.Register("Text",
                                               typeof(string),
                                               typeof(IconButton),
                                               new PropertyMetadata("Button text", OnTextChanged));

    MDL2IconCodeProperty = DependencyProperty.Register("MDL2IconCode",
                                                       typeof(string),
                                                       typeof(IconButton),
                                                       new PropertyMetadata("\uf13e", OnIconTextChanged));
  }

  static void OnTextChanged(DependencyObject o,
                            DependencyPropertyChangedEventArgs e)
  {
    var iconButton = o as IconButton;
    if (iconButton == null)
    {
      return;
    }
    string newText = e.NewValue as string;
    iconButton.Text = newText;
  }

  static void OnIconTextChanged(DependencyObject o,
                                DependencyPropertyChangedEventArgs e)
  {
    var iconButton = o as IconButton;
    if (iconButton == null)
    {
      return;
    }

    string newText = e.NewValue as string;
    iconButton.MDL2IconCode = newText;
  }
}

Generic.xaml的ResourceDictionary看起来像这样:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UI.CustomControls">


  <Style TargetType="{x:Type local:IconButton}" 
         BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:IconButton}">
          <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
            <StackPanel>
              <TextBlock HorizontalAlignment="Center"
                         Text="{TemplateBinding MDL2IconCode}"
                         FontFamily="Segoe MDL2 Assets"
                         FontSize="16"
                         x:Name="iconTextBlock"/>
              <TextBlock HorizontalAlignment="Center" 
                         Text="{TemplateBinding Text}"
                         x:Name="textTextBlock"/>
            </StackPanel>

          </Button>

        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

该按钮看起来应有。

但是XAML中的命令绑定不再起作用。但是它应该可以工作,因为继承仍然是一个按钮。

也许任何人都知道要添加什么以使命令绑定起作用?

那个家伙

Button将控制模板内部的命令绑定到模板化父对象。

<ControlTemplate TargetType="{x:Type local:IconButton}">
   <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
           Command="{TemplateBinding Command}"
           CommandParameter="{TemplateBinding CommandParameter}"
           CommandTarget="{TemplateBinding CommandTarget}">
      <!-- ...other code. -->
   </Button>
</ControlTemplate>

但是它应该可以工作,因为继承仍然是一个按钮。

Button不能。您的控件模板内部不会神奇地绑定到其模板化父对象的相应属性,无论它是派生自Button其他控件还是其他控件。您还必须对其他依赖项属性(例如)执行此CommandParameter操作。

还请注意,这TemplateBinding是一种优化的绑定,不具有更强大的Binding标记扩展的所有功能因此,当TemplateBinding无法使用时(例如在双向绑定方案中),可以这样使用TemplatedParent

{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MyDependencyProperty}

有关更多信息,您可以参考TemplateBinding文档。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

子控件未继承 WPF 自定义控件样式

来自分类Dev

WPF、自定义控件和样式/主题继承

来自分类Dev

使用fxml从自定义控件继承

来自分类Dev

具有从ListBox WPF继承的选中对象逻辑的自定义控件

来自分类Dev

WPF自定义控件,模板,继承和依赖项属性

来自分类Dev

自定义控件是否继承基类INotifyPropertyChanged接口?

来自分类Dev

从嵌套自定义控件中的RepeatControl继承索引

来自分类Dev

创建自定义构造函数而不直接调用继承

来自分类Dev

自定义元素继承

来自分类Dev

自定义元素继承

来自分类Dev

如何从继承自另一个自定义控件的自定义控件中触发方法?

来自分类Dev

WPF KeyBinding 命令未在自定义控件中触发

来自分类Dev

WPF控件的继承无效

来自分类Dev

WPF控件的继承无效

来自分类Dev

使用HBox容器和继承但自定义的事件扩展控件

来自分类Dev

如何在 xaml 中继承用 C# 编写的自定义控件

来自分类Dev

Odoo中自定义模块的继承

来自分类Dev

如何从自定义元素继承

来自分类Dev

openerp中自定义模块的继承

来自分类Dev

演示自定义异常继承Java

来自分类Dev

UILabel继承的Swift自定义UIView

来自分类Dev

自定义 WPF 控件的好处

来自分类Dev

WPF 样式自定义控件

来自分类Dev

WPF自定义控件-绑定到在后台代码中定义的命令

来自分类Dev

WPF用户控件继承问题

来自分类Dev

WPF用户控件继承问题

来自分类Dev

为什么自定义控件继承自Control但不继承自Button时,这些触发器/事件为何起作用?

来自分类Dev

使Django表单继承自定义字段属性的类

来自分类Dev

从TButton继承的自定义按钮不显示