如何使用相同的键来处理不同的目标类型 [英] How works with a same key for different target types

查看:56
本文介绍了如何使用相同的键来处理不同的目标类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它可能吗?



我不明白为什么这段代码工作正常:



< ;样式x:Key =MyStyleTargetType ={x:Type Canvas}>

< Setter Property =WidthValue =125/>

< Setter Property =HeightValue =55/>

< Setter Property =BackgroundValue =White/>

< / Style>



但是这个错了:



< Style x:Key =MyStyle>

< Style.Resources>

< Style TargetType =Canvas>

< Setter Property =宽度值=125/>

< Setter Property =HeightValue =55/>

< Setter Property =背景值=白色/>

< / Style>

< /Style.Resources>

< / Style> ;



我的尝试:



案例是我想在最后的MyStyle中注入几种类型,例如文本框,省略号等。



我解释了自己那么?

解决方案

如果你想让同一个样式使用多种类型,你实际上需要创建一个以这些控件的基本类型为目标的样式。您不必将自己限制在最派生类型。例如,您可以为所有Panel类型(Canvas等)定义样式,如下所示:

< Style x:Key =   PanelStyle TargetType =   {x:Type Panel}  >  
< Setter Property = 宽度值= 125 />
< Setter Property = 高度值= 55 />
< / 风格 >


Its possible ?

I don't understand why this code works fine:

<Style x:Key="MyStyle" TargetType="{x:Type Canvas}">
<Setter Property="Width" Value="125" />
<Setter Property="Height" Value="55" />
<Setter Property="Background" Value="White" />
</Style>

But this one its wrong:

<Style x:Key="MyStyle">
<Style.Resources>
<Style TargetType="Canvas">
<Setter Property="Width" Value="125" />
<Setter Property="Height" Value="55" />
<Setter Property="Background" Value="White" />
</Style>
</Style.Resources>
</Style>

What I have tried:

The case is I want to inject several types in this last MyStyle, such as textboxes, ellipses, etc..

I explained myself well?

解决方案

If you want the same style to work with multiple types, you actually need to create a style that targets a base type of those controls. You don't have to constrain yourself to the most derived type. For instance, you could define a style for all Panel types (Canvas, etc), like this:

<Style x:Key="PanelStyle" TargetType="{x:Type Panel}">
  <Setter Property="Width" Value="125" />
  <Setter Property="Height" Value="55" />
</Style>


这篇关于如何使用相同的键来处理不同的目标类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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