Friday, 9 August 2013

WPF Style is not applied for a UserControl who is already styled

WPF Style is not applied for a UserControl who is already styled

I come to you because I got headaches about control styling for a few
hours. By defining a style to the usercontrol, it doesn't work !
My usercontrol declaration :
<uiComponent:NumericTextBox Text="{Binding myProperty}"/>
The style I want to apply to :
<Style TargetType="uiComponent:NumericTextBox">
<Setter Property="Background" Value="Black"/>
</Style>
Why it doesn't work with the Background property, although it works with
the Visibility property ! I tried with TargetType=FrameworkElement, no
effect....
My usercontrol is a numerictextbox which define its own style like this :
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:l="clr-namespace:LSX.Space.PropertyUI.NumericTextBox">
<SolidColorBrush x:Key="CustomTextBox_Background" Color="White" />
<SolidColorBrush x:Key="CustomTextBox_Foreground" Color="Black" />
<LinearGradientBrush x:Key="CustomTextBox_Border" StartPoint="0,0"
EndPoint="0,1">
<GradientStop Color="#FFABADB3" Offset="0.05" />
<GradientStop Color="#FFE2E3EA" Offset="0.07" />
<GradientStop Color="#FFE3E9EF" Offset="1" />
</LinearGradientBrush>
<Style x:Key="{x:Type l:NumericTextBox}" TargetType="{x:Type
l:NumericTextBox}">
<Setter Property="Background" Value="{StaticResource
CustomTextBox_Background}" />
<Setter Property="BorderBrush" Value="{StaticResource
CustomTextBox_Border}" />
<Setter Property="Foreground" Value="{StaticResource
CustomTextBox_Foreground}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:NumericTextBox}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="LayoutGrid">
<ScrollViewer Margin="2"
x:Name="PART_ContentHost" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--Message validation des erreurs-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasText" Value="True" />
<Condition Property="Validation.HasError"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="ToolTip" Value="{Binding
RelativeSource={RelativeSource Self}, Path=
TextError}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Image x:Name="ValidationIcon"
DockPanel.Dock="Left"
Stretch="None" Width="15"
Height="15"
Source="pack://application:,,,/LS.Net.Telcom.Space.PropertyUI;component/Images/validationError.png"
/>
<Border BorderBrush="Red"
BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Many thanks for your help.

No comments:

Post a Comment