我创建了一个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] 删除。
我来说两句