永远不会调用OnApplyTemplate()

时间:2011-05-13 13:57:42

标签: wpf controls styles

我为自定义按钮控件创建了一个样式。此样式位于COmmon项目中的ResourceDictionary.xaml(具有所有自定义控件样式的文件)中。我在项目中添加了对此COmmon项目的引用,并在我的xaml文件中设置了以下内容:

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

Tha实际风格如下所示。问题是TMCVCARSButton.cs中的OnApplyTemplate永远不会被调用。有什么想法吗?

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

感谢。

2 个答案:

答案 0 :(得分:1)

我的猜测是问题在于你的其他一些代码。屏幕上是否显示您正在使用控件的任何内容?当您向我们展示您在XAML文件中设置的代码时,该文件是哪个?是与控件正在使用的文件相同,还是在Application.xaml文件中?最后,如果你在ResourceDictionary中放置了一些无效的控件模板并运行应用程序它会显示错误(这将告诉我们字典是否实际上正确合并)

答案 1 :(得分:0)

尝试删除x:Key属性。我之前遇到了同样的问题,删除了x:Key在这种情况下有帮助,我在那里定位给定类型的控件。根据我的(有限)理解,通过在样式中使用x:Key属性,WPF期望控件显式引用样式。

您还需要在项目的Themes文件夹中的Generic.xaml中或应用程序启动合并词典中引用资源字典到应用程序的资源字典中。