2

我有这个带有自定义颜色属性的库。我希望能够像这样在 XAML 中使用这些属性:

    <Style TargetType="{x:Type eg:MyWindow}">
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="CustomClass.CustomColorProperty"/>
            </Setter.Value>
        </Setter>
    </Style>

包含CustomClass的命名空间已被引用。我该怎么办?谢谢。

编辑:

我刚刚注意到CustomClass是静态的,所以我无法在 XAML 中创建它的实例。此外,当我输入eg:时,CustomClass不会出现在智能感知中。如果我有一个实例类,我无法让您的任何解决方案起作用,即使它们应该起作用。这种情况有解决方法吗?

编辑2:

这是实际的类和命名空间:

namespace Assergs.Windows
{
    public static class OfficeColors
    {
        public class Background
        {
            public static Color OfficeColor1 = (Color)ColorConverter.ConvertFromString("#e4e6e8");
            public static Color OfficeColor2 = (Color)ColorConverter.ConvertFromString("#dce0ed");
            public static Color OfficeColor3 = (Color)ColorConverter.ConvertFromString("#a8c3e0");
        }
    }
}

这是 XAML 命名空间:

xmlns:aw="clr-namespace:Assergs.Windows;assembly=Assergs.Windows"

如果我按照 Zenuka 的建议使用这条线:

<SolidColorBrush Color="{x:Static aw:OfficeColors.Background.OfficeColor1}"/>

它在编译时抛出此错误:

Cannot find the type 'OfficeColors.Background'. Note that type names are case sensitive.
4

3 回答 3

4

用这个:

<SolidColorBrush Color="{x:Static aw:OfficeColors+Background.OfficeColor1}"/>

注意 + 符号而不是点来引用嵌套类

于 2009-10-28T10:07:14.540 回答
2

我假设您在 CustomClass 上有一个静态属性?然后你可以使用:

<SolidColorBrush Color="{x:Static eg:CustomClass.CustomColorProperty}"/>

但也许您需要更改命名空间前缀...

编辑:
问题在于你在另一个类中声明一个类......我建议你将类 Backgroud 移到 OfficeColors 类之外并将其声明为静态或将背景类的属性移动到 OfficeColors 类(可能带有背景前缀),或者在您尝试时使用名称空间。

玩得开心 :)

EDIT2:
使用 Nir ​​的方法,使用 + 号 'aw:OfficeColors+Background.OfficeColor1' 来引用嵌套类,不知道那个:)

于 2009-10-28T07:38:55.503 回答
1

您必须将类的一个实例声明为资源之一。(假设 CustomColorProperty 不是静态的)

<CustomNamespace.CustomClass x:Key=CcInstance />
<Style TargetType="{x:Type eg:MyWindow}">        
     <Setter Property="Background">            
         <Setter.Value>                
              <SolidColorBrush Color="{Binding Source={StaticResource CcInstance}, Path=CustomColorProperty} />            
         </Setter.Value>        
     </Setter>    
</Style>
于 2009-10-28T07:40:28.980 回答