隐式样式不适用于定义了默认样式的自定义无外观控件 [英] Implicit style will not apply to custom lookless control with default style defined

查看:69
本文介绍了隐式样式不适用于定义了默认样式的自定义无外观控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的无视控件,用于定义一组常用功能以及项目中所有页面的通用外观。我添加了相应的默认样式键覆盖:

I have created a custom, lookless control to define a set of common functionalities and a common look for all pages in my project. I added the appropriate default style key override:

static BasePageView()
{  
   FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(BasePageView), new FrameworkPropertyMetadata(typeof(BasePageView)));
}

添加了必需的主题信息属性:

added the required theme info attribute:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None,
    ResourceDictionaryLocation.SourceAssembly
)]

并在generic.xaml中创建了一个默认样式,如下所示:

and created a default style in the generic.xaml like so:

   <Style TargetType="this:BasePageView">
        <Setter Property="Padding" Value="15,10"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="this:BasePageView">
                     <Border Background="{TemplateBinding Background}">
                         <ContentPresenter Margin="{TemplateBinding Padding}"/>
                     </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我已经确认默认样式已正确应用。

I have verified that the default style gets applied correctly.

此控件的意图是允许我的库的消费者使用和重新设计他们认为合适的样式,类似于任何内置的WPF控件。为了验证这个功能,我检查了是否应该适当地应用了应用程序范围的隐式样式
所以我将以下样式添加到我的app.xaml:

The intention of this control is to allow consumers of my library to use and re-style as they see fit, similar to any built-in WPF controls. To verify this functionality, I checked to see if an application-wide implicit style will be applied appropriately so I added the following style to my app.xaml:

   <Style TargetType="this:BasePageView">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Background" Value="Red"/>
    </Style>

我发现这个隐式样式从未在UI中显示过,但是在查看Live Visual Tree资源管理器时,我发现此样式应用为"默认"到我的BasePageViews的实例:

What I found was that this implicit style was never shown in the UI, however when looking at the Live Visual Tree explorer, I found that this style was applied as "default" to the instances of my BasePageViews:

如果正确应用隐式样式,我希望看到如下内容:

whereas I would expect to see something like the following if the implicit style is correctly applied:

我可以通过看似黑客的方式开展工作,因为我无法找到这种逻辑在任何.NET源代码中:

Which I can get to work by what seems like a hack because I have not been able to find this sort of logic in any .NET source code:

var style = this.TryFindResource(typeof(BasePageView)) as Style;

if (style != null)
   this.Style = style;

SetResourceReference(StyleProperty, typeof(BasePageView));

我想知道的是:什么是正确 在应用默认样式后获取隐式样式的方法?我已经按照我找到的每个教程进行了操作,包括  控制
创作概述
,我似乎仍然无法复制所有其他WPF控件所遵循的内置样式功能,这是非常令人愤怒的。我甚至根据控件创作概述中的建议使我的BasePageView继承自Control和ContentControl而不是UserControl
,但这没有任何区别。我可以忍受hack,但是不应该这样做。

What I want to know is: What is the correct way to get implicit styles to apply after the default style is applied? I have followed every tutorial I have found, including the Control Authoring Overview and I still cannot seem to duplicate the built-in styling functionality that all other WPF controls follow, which is positively infuriating. I have even made my BasePageView inherit from Control and ContentControl instead of UserControl as suggested in the control authoring overview, but that made no difference. I can live with the hack, however there should be no need to do this.

我还发现,如果我在从BasePageView派生的控件的资源中包含隐式样式,该样式也未正确应用:

I have also found that if I include the implicit style within the resources of a control that derives from BasePageView, the style is also not applied correctly:

<Controls:BasePageView
    x:Class="Pages.SequencePageView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Controls;assembly=Controls">

    <Control.Resources>
        <Style TargetType="Controls:BasePageView">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Background" Value="Red"/>
        </Style>
    </Control.Resources>

   <!-- Content -->

</Controls:BasePageView>

推荐答案

嗨Tanner,

Hi Tanner,

根据你的描述,你有两种样式用于BasepageView,一种样式是BasedPageView的默认样式,另一种样式是隐式的。

According to your description, you have two styles for BasepageView, one style is default style for BasedPageView, another style is implicit.

如果使用第一种样式(默认样式),你不能用另一个隐式样式覆盖那个样式。

If you use the first style(default style), you can't overwrite that style with another implicit style.

我做了一个例子,因为我不知道什么是BasePageView,我在这里使用Button。

I do one example ,because I don't know what is BasePageView, I use Button here.

 <Button
                Name="btn1"
                Width="200"
                Height="30"
                Margin="0,10,0,10"
              
                Content="btn1">
                <Button.Resources>
                    <Style TargetType="Button">
                        <Setter Property="Padding" Value="15,10" />
                        <Setter Property="Background" Value="Blue" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <Border Background="{TemplateBinding Background}">
                                        <ContentPresenter Margin="{TemplateBinding Padding}" />
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                    </Style>
                </Button.Resources>
            </Button>

您在App.xaml中添加以下样式,它不会应用于此按钮。它为Button选择一种样式。

The you add the following style in App.xaml, it won't be applied to this button. it choose one style for Button.

如果你想创建新的Button,它首先应用app.xaml样式,但如果你为按钮添加另一种样式,我将丢失该应用程序。 xaml风格。

If you want to create new Button, it apply the app.xaml style firstly, but if you add another style for button, I will lose the app.xaml style.

最好的问候,

Cherry


这篇关于隐式样式不适用于定义了默认样式的自定义无外观控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆