访问 XAML 中的静态字段

Posted

技术标签:

【中文标题】访问 XAML 中的静态字段【英文标题】:Accessing static fields in XAML 【发布时间】:2010-09-07 03:20:22 【问题描述】:

如何在 xaml 中引用类的静态属性?换句话说,我想做这样的事情:

Class BaseThingy 
  public static readonly Style BaseStyle;
  ...

<ResoureDictionary ...>
  <Style BasedOn="BaseThingy.Style" TargetType="BaseThingy" />
</ResourceDictionary>

在 BasedOn 中执行此操作的语法是什么?我认为它会在某种程度上涉及使用StaticResource,但我还没有让它为我工作。

【问题讨论】:

【参考方案1】:

使用x:Static 标记扩展

<ResoureDictionary ...
  xmlns:local="clr-namespace:Namespace.Where.Your.BaseThingy.Class.Is.Defined"
>
  <Style BasedOn="x:Static local:BaseThingy.BaseStyle" TargetType="BaseThingy" />
</ResourceDictionary>

【讨论】:

我的印象是x:Static ... 在 Silverlight 中不起作用。 Is Namespace.Where.Your.BaseThingy.Class.Is.Defined 类的命名空间?

以上是关于访问 XAML 中的静态字段的主要内容,如果未能解决你的问题,请参考以下文章