在XAML中访问静态字段

时间:2020-03-05 18:44:24  来源:igfitidea点击:

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

Class BaseThingy {
  public static readonly Style BaseStyle;
  ...
}
<ResoureDictionary ...>
  <Style BasedOn="BaseThingy.Style" TargetType="BaseThingy" />
</ResourceDictionary>

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

解决方案

回答

使用x:静态标记扩展

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