具有自定义控件的依赖项属性未显示

时间:2018-12-08 19:08:53

标签: c# wpf custom-controls mahapps.metro

我有一个自定义控件:

<Button x:Class="TESterUI.CustomControls.ToolbarButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:TESterUI.CustomControls"
        xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        mc:Ignorable="d"
        Height="35" Width="35">
    <StackPanel Orientation="Vertical">
        <iconPacks:PackIconModern Kind="{Binding TbIcon}"/>
        <TextBlock Text="{Binding TbText}" FontSize="12"/>
    </StackPanel>
</Button>

及其支持:

public partial class ToolbarButton : Button
{
    public ToolbarButton()
    {
        InitializeComponent();
    }

    public string TbText
    {
        get { return (string)GetValue(TbTextProperty); }
        set { SetValue(TbTextProperty, value); }
    }

    public static readonly DependencyProperty TbTextProperty =
        DependencyProperty.Register("TbText", typeof(string), typeof(Button), new PropertyMetadata(null));

    public PackIconModernKind TbIcon
    {
        get { return (PackIconModernKind)GetValue(TbIconProperty); }
        set { SetValue(TbIconProperty, value); }
    }

    public static readonly DependencyProperty TbIconProperty =
        DependencyProperty.Register("TbIcon", typeof(PackIconModernKind), typeof(Button), new PropertyMetadata(null));
}

和实现:

<customs:ToolbarButton TbText="Day" TbIcon="CalendarDay"/>

但是显示的按钮没有图标,也没有文本。这是我第一次接触自定义控件,我不确定该去哪里。我试图将其设置为字符串属性,并使用Parse从enum进行转换,但这也不起作用,然后我了解到WPF具有内部字符串到枚举的转换,因此我将所有转换都删除了。我认为,至少应该显示文字。

0 个答案:

没有答案
相关问题