ygoe
ygoe

Reputation: 20354

Explicit Style ignored for derived control in a DataTemplate

Some styles don't work as expected here. This is the demo style, it makes the background orange to prove that it's been applied to a control:

<Style x:Key="OrangeTestStyle" TargetType="TextBox">
    <Setter Property="Background" Value="Orange"/>
</Style>

I can use it like this just normally for TextBox and also for my own class derived from TextBox:

<TextBox Style="{StaticResource OrangeTestStyle}"/><!-- orange -->
<ui:UnitTextBox Style="{StaticResource OrangeTestStyle}"/><!-- orange -->

Both will have an orange background. But it is not applied for the derived control if it's inside a DataTemplate. In fact no explicitly set style at all is applied to any derived control. Standard framework controls work just fine:

<DataTemplate>
    <StackPanel>
        <TextBox Style="{StaticResource OrangeTestStyle}"/><!-- orange -->
        <ui:UnitTextBox Style="{StaticResource OrangeTestStyle}"/><!-- white -->
    </StackPanel>
</DataTemplate>

My derived control doesn't override anything related to Style. It just adds a new dependency property and displays its content in additional visual elements, it's kind of a decoration. But it's still derived directly from TextBox. Styles also work in the derived control, but not if inside a template. Styles also work in a template, but not on derived controls. (I have other derived controls in my application.)

What's the problem here?

.NET 4.0, Visual Studio 2010, Windows 7.

Upvotes: 0

Views: 61

Answers (1)

Cesario
Cesario

Reputation: 172

Removing the SetResourceReference(StyleProperty, typeof(TextBox))from your UnitTextBox constructor should fix it.

Upvotes: 1

Related Questions