OnApplyTemplate () is never called

I created a style for custom button controls. This style is in ResourceDictionary.xaml (A file that has all user-defined management styles) in a COmmon project. I added a link to this COmmon project in my project, and also set the following in my xaml file:

<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/UTS.Pearl.BO.UI.Common;component/Styles/ResourceDictionary.xaml"/> </ResourceDictionary.MergedDictionaries> 

The real style is given below. The problem is OnApplyTemplate in TMCVCARSButton.cs is never called. Any ideas?

 <Style x:Key="{x:Type common:TMCVCARSButton}" BasedOn="{x:Null}" TargetType="{x:Type common:TMCVCARSButton}"> <Setter Property="FontFamily" Value="Arial" /> <Setter Property="FontSize" Value="12" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type common:TMCVCARSButton}"> <Grid Width="160" Margin="0,0,2,2"> <Rectangle RenderTransformOrigin="0.485,1" x:Name="rectangle" RadiusX="5" RadiusY="5" Opacity="1" Stroke="{DynamicResource Dark Gray}" StrokeThickness="1"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.037,-0.047" StartPoint="0.037,1.418"> <GradientStop Color="#FFCBC4C0" Offset="0.826"/> <GradientStop Color="#FFFDFBFB" Offset="1"/> <GradientStop Color="#FFDBD7D4" Offset="0"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Viewbox Stretch="Fill" StretchDirection="DownOnly" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="viewbox" OpacityMask="{x:Null}" > <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" x:Name="contentPresenter" Visibility="Collapsed"/> </Viewbox> <WrapPanel Margin="4" Background="{x:Null}"> <Grid x:Name="_grdFrontLeft" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" MinHeight="75" Background="{x:Null}" Margin="0,2,2,0"> <Rectangle Stroke="Black" StrokeThickness="0.5"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,2.044" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Viewbox x:Name="_vbxVCARSImageFrontLeft" Margin="0.5"> <Image x:Name="_imgFrontLeft" Width="Auto" Height="Auto"/> </Viewbox> </Grid> <Grid x:Name="_grdFrontRight" HorizontalAlignment="Left" Margin="0,2,0,0" MinHeight="75" MinWidth="75" VerticalAlignment="Top" Height="Auto" Background="{x:Null}"> <Rectangle Stroke="Black" StrokeThickness="0.5"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,2.044" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Viewbox x:Name="_vbxVCARSImageFrontRight" Margin="0.5"> <Image x:Name="_imgFrontRight" Width="Auto" Height="Auto"/> </Viewbox> </Grid> <Grid x:Name="_grdRearLeft" HorizontalAlignment="Left" Margin="0,2,2,0" MinHeight="75" MinWidth="75" VerticalAlignment="Top" Height="Auto" Background="{x:Null}"> <Rectangle Stroke="Black" StrokeThickness="0.5"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,2.044" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Viewbox x:Name="_vbxVCARSImageRearLeft" Margin="0.5"> <Image x:Name="_imgRearLeft" Width="Auto" Height="Auto"/> </Viewbox> </Grid> <Grid x:Name="_grdRearRight" HorizontalAlignment="Left" Margin="0,2,0,0" MinHeight="75" MinWidth="75" VerticalAlignment="Top" Height="Auto" Background="{x:Null}"> <Rectangle Stroke="Black" StrokeThickness="0.5"> <Rectangle.Fill> <LinearGradientBrush EndPoint="0.5,2.044" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Viewbox x:Name="_vbxVCARSImageRearRight" Margin="0.5"> <Image x:Name="_imgRearRight" Width="Auto" Height="Auto"/> </Viewbox> </Grid> </WrapPanel> </Grid> 

Thanks.

0
controls styles wpf
source share
2 answers

My guess is that the problem is with some of your other codes. Does anything appear on the screen where you are using the control? When you show us the code that you said is installed in your XAML file, what file is it? Is this the same file the control is used in, or is it in your Application.xaml file? Finally, if you put something invalid in your ResourceDictionary with the control template and run the application, it displays an error (this will let us know if the Dictionary really merges correctly)

+1
source share

Try removing the x: Key attribute. I used to have the same problem, so in this case x: Key was removed, where I was setting up controls for a specific type. From my (limited) understanding, using the x: Key attribute in a style, WPF expects the style to be explicitly specified by the control.

You also need to reference the resource dictionary in Generic.xaml in the Themes folder in your project or in the merge dictionaries when you run the application in the application resource dictionary.

0
source share

All Articles