WPF类和相应的视觉样式继承 [英] WPF Class and corresponding Visual Style Inheritance

查看:191
本文介绍了WPF类和相应的视觉样式继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过,但显然无法在与类和样式相关时获得正确的语法。我有一些具有某些行为的控件。我推导出一些额外的行为。现在,我想要一个样式对应每个版本。在最简单的例子中,我将忽略这些类,因为我知道样式与视觉和非功能影响特别相关。之前的一些问题很接近,但我仍然遗漏了一些东西。

I've looked but apparently can't get the syntax correct when correlating to classes and styles. I have controls that have certain behavior. Some I derive to add additional behavior. Now, I want a style to correspond to each version. In its simplest example, I will ignore those classes as I know style is specifically associated to visual and not function impact. Some previous questions were close, but I am still missing something.

public class MyLabel : Label
{}

public class MyLabel2 : MyLabel
{}

所以从上面,我应该能够在我的资源字典中有3个样式。一个用于通用标签,一个用于我具有从MyLabel派生的控件而另一个用于MyLabel2的实例。根据主题xaml声明,local:指向我的类库。

So from the above, I should be able to have 3 styles in my resource dictionary. One for the generic "Label", one for instances where I have a control that is derived from "MyLabel", and another for "MyLabel2". The "local:" points to my class library per the "Theme" xaml declaration.

<Style x:Key="baseLabel" TargetType="Label">
   <Setter Property="FontSize" Value="10" />
</Style>

<Style x:Key="styleMyLabel" TargetType="local:MyLabel"  BasedOn="{StaticResource baseLabel}" >
   <Setter Property="FontSize" Value="14" />
</Style>

<Style x:Key="styleMyLabel2" TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
   <Setter Property="FontSize" Value="22" />
</Style>

同样,出于非常简单的目的,只需使用简单的标签进行继承。在我的窗口xaml文件中。根据窗口xaml,src:指向我的类库,它与主题中引用的local:相同(但我尝试了两种方式...让两个实例引用xmlns:local in两者,但它似乎没有区别)。

Again, for very simplistic purposes, just using a simple label for inheritance following purposes. In my window xaml file. Per the window xaml, the "src:" points to my class library, which is the same library as "local:" referenced in the theme (but I tried both ways... to have both instances refer to the xmlns:local in both, but it didn't appear to make a difference).

<src:MyLabel Content="Does Not Respect Style even though derived from MyLabel class" />
<src:MyLabel Content="This one works " Style="{DynamicResource styleMyLabel}" />

<src:MyLabel2 Content="This one doesnt work"/>
<src:MyLabel2 Content="This one works " Style="{DynamicResource styleMyLabel2}" />

我想我不应该明确地识别它的风格,如果它来自一个类具有相应目标类型的样式。希望它的东西很简单,但我不能把它缠在它上面......

I'm thinking I shouldn't have to explicitly identify the style if its derived from a class that has a style of the corresponding Target Type. Hopefully its something stupid simple but I can't get my head wrapped around it...

推荐答案

好的,我绊倒了进入什么样的APPEARS作为答案,但不明白为什么......如果我试图改变MyLabel2(这是第二个派生实例)的样式来自

Ok, I've stumbled into what APPEARS to be working as the answer, but don't understand why... If I try to change the style for the MyLabel2 (which is the second derived instance) from

<Style x:Key="styleMyLabel2" TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
   <Setter Property="FontSize" Value="22" />
</Style>

to(只删除x:Key元素)

to (just removing the x:Key element)

<Style TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
   <Setter Property="FontSize" Value="22" />
</Style>

XAML正常工作......无需明确关联要使用的样式。它直接根据类关联找到它。

the XAML works... WITHOUT having to explicitly associate the "Style" to be used. It finds it based on the class association directly.

<src:MyLabel2 Content="Now Works as expected"/>

既然它有效,有人可以解释为什么额外使用x:Key引用会杀死它?即:如果您有x:键,它不是由类实例暗示,但没有它,直接类会自动找到它。

Now that it works, can someone maybe explain why the extra use of the x:Key reference kills it? ie: if you have the x:key, its not implied by the class instance, but not having it, the direct class DOES find it automatically.

EXPANDED从我在风格中玩耍......

所以,这里有一些我发现的额外内容。一个样式可以多次定义到相同的TARGET TYPE ...但是,一个可以有一个x:key引用,另一个可以有一个BasedOn引用。这个APPEARS就像重载具有相同数字但不同数据类型参数的函数一样。

So, here is some extras that I've found out. A style can be defined more than once to the same TARGET TYPE... However, one can have an x:key reference, and another a BasedOn reference. This APPEARS to be like overloading a function with same number, but different data type parameters.

在我的问题中,只要添加了x:Key引用样式,TargetType的任何直接类实例都不会自动链接到正确的样式,几乎就像带有x:key的样式声明一样,需要类的任何实例在xaml中明确添加样式上下文。控制。那么现在,如何实现这两种方式。我创建了一个以TargetType作为基类的SECOND样式,但是ITs BasedOn指向了x:key引用的样式。这样,我得到了样式的默认自定义,但是仍然可以将它分配给基类和派生类,因此控件的所有3个版本都可以自动与相应的样式同步而无需EXPLICIT引用。

In my problem, as soon as the "x:Key" reference was added to the style, any direct class instances of the "TargetType" were NOT automatically linked up to the proper style, almost like the style declaration with the "x:key" REQUIRES any instances of a class to explicitly add the style context within the xaml of the control. So now, how to have it BOTH ways. I created a SECOND style with the TargetType as the baseclass, but had ITs BasedOn pointing to the x:key referenced one. This way, I get the default customization of the style, yet can STILL assign it to the baseclass and the derived class as well, so all 3 versions of a control can be automatically synchronized with the corresponding Style without EXPLICIT referencing.

<Style TargetType="Label" x:Key="wpfBaseLabel">
   <Setter Property="FontSize" Value="11" />
</Style>

<Style TargetType="Label" BasedOn="{StaticResource wpfBaseLabel}" />

<Style TargetType="{x:Type local:MyLabel}" BasedOn="{StaticResource wpfBaseLabel}">
   <Setter Property="Foreground" Value="Blue" />
</Style>

所以,现在,在最终的XAML中,我可以使用或不使用所有3个类显式样式关联。

So, now, within the final XAML, I can have all 3 "classes" used with or without explicit style association.

这篇关于WPF类和相应的视觉样式继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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