Question

I have created this control in 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); }
    }
}

Also, I defined a default template for the control in /Themes/Generic.xaml in project:

<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>

But when I used it in a view, it's not shown. Where am I wrong? Any idea please?

Was it helpful?

Solution

you have to override the DefaultStyleKey metadata like below, to indicate you have defined the style in Generic.xaml

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

Add this code inside the static constructor of your control

OTHER TIPS

Ooops! I found the problem. I just forgot to override meta-data. Put it here for helping somebody else if has the same problem.

// in static .ctor
static TextElement() {
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof (TextElement), new FrameworkPropertyMetadata(typeof (TextElement)));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top