0

我在以下位置创建了这个控件WPF

[ContentProperty("Text")]
[DefaultProperty("Text")]
public class TextElement : Control {

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), 
        typeof(TextElement), new PropertyMetadata(default(string)));

    public static readonly DependencyProperty LabelProperty =
        DependencyProperty.Register("Label", typeof(string), 
        typeof(TextElement), new PropertyMetadata(default(string)));

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

    [Bindable(true)]
    public string Label {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }
}

另外,我在项目中为控件定义了一个默认模板/Themes/Generic.xaml

<Style x:Key="{x:Type local:TextElement}" TargetType="{x:Type local:TextElement}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrush}}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrush}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TextElement}">
                <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalAlignment}"
                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                      Background="Yellow"
                      Width="100" Height="30">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="55"/>
                        <ColumnDefinition Width="66"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" x:Name="label" Background="Red" Height="20" Width="20"
                           HorizontalAlignment="Stretch"
                           HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                           VerticalAlignment="Stretch"
                           VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                           Content="{TemplateBinding Label}"
                           Style="{TemplateBinding LabelStyle}"/>
                    <ContentPresenter />
                    <TextBlock Text="{TemplateBinding Text}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是当我在视图中使用它时,它没有显示出来。我哪里错了?请问有什么想法吗?

4

2 回答 2

1

您必须重写 DefaultStyleKey 元数据,如下所示,以表明您已在 Generic.xaml 中定义样式

DefaultStyleKeyProperty.OverrideMetadata(typeof(TestItemsControl), new FrameworkPropertyMetadata(typeof(TestItemsControl)));

将此代码添加到控件的静态构造函数中

于 2013-07-26T11:36:52.047 回答
0

哎呀!我发现了问题。我只是忘了覆盖元数据。如果有同样的问题,把它放在这里帮助别人。

// in static .ctor
static TextElement() {
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof (TextElement), new FrameworkPropertyMetadata(typeof (TextElement)));
}
于 2013-07-26T11:37:53.290 回答