OnApplyTemplate() 从未被调用

发布于 2024-11-06 20:54:26 字数 6656 浏览 0 评论 0原文

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

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

下面列出了实际的样式。问题是 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>

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2024-11-13 20:54:26

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

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)

£冰雨忧蓝° 2024-11-13 20:54:26

尝试删除 x:Key 属性。我之前也遇到过同样的问题,删除 x:Key 在这种情况下有所帮助,我的目标是给定类型的控件。根据我(有限)的理解,通过在样式中包含 x:Key 属性,WPF 希望控件显式引用该样式。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文