Reputation: 345
I created a style for a custom button control. This style is in ResourceDictionary.xaml(File which has all custom control styles) in COmmon project. I added the reference 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>
Tha actual style is listed below. The problem is OnApplyTemplate in the TMCVCARSButton.cs is never being 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.
Upvotes: 0
Views: 3698
Reputation: 2944
My guess is that the problem lies in some of your other code. Is anything showing up on the screen where you are using the control? When you show us the code that you said you set in your XAML file, which file is that? Is that the same file that the control is being used on, or is it in your Application.xaml file? Lastly if you put something invalid in your ResourceDictionary that has the control template and run the application does it display the error (this will let us know if the Dictionary is in fact getting merged correctly)
Upvotes: 1
Reputation: 935
Try removing the x:Key attribute. I had the same problem earlier, removing the x:Key helped in this case, where I was targeting controls of a given type. From my (limited) understanding, by having the x:Key attribute in the style, WPF expects the style to be explicitly referenced by the control.
You also need to reference the resource dictionary in the Generic.xaml in the Themes folder in your project or on app startup merge dictionaries into the application's resource dictionary.
Upvotes: 1