如何将自定义属性设置为 XAML 值?
我有这个带有自定义颜色属性的库。我希望能够在 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.
I have this library with custom Color properties. I wanna be able to use these properties in XAML like this:
<Style TargetType="{x:Type eg:MyWindow}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="CustomClass.CustomColorProperty"/>
</Setter.Value>
</Setter>
</Style>
The namespace that contains CustomClass is already referenced. How should I go about this? Thanks.
EDIT:
I just noticed that CustomClass is static, so I can't create an instance of it in XAML. Also, when I type eg:, CustomClass doesn't show up in intellisense. I can't get any of your solutions to work, even though they should, if I had an instance class. Is there a workaround for this situation?
EDIT 2:
This is the actual class and namespace:
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");
}
}
}
And this is the XAML namespace:
xmlns:aw="clr-namespace:Assergs.Windows;assembly=Assergs.Windows"
And if I use this line, as suggested by Zenuka:
<SolidColorBrush Color="{x:Static aw:OfficeColors.Background.OfficeColor1}"/>
It throws this error at compile time:
Cannot find the type 'OfficeColors.Background'. Note that type names are case sensitive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用这个:
注意使用 + 号而不是点来引用嵌套类
Use this:
Notice the + sign instead of a dot to reference nested classes
我假设您在 CustomClass 上有一个静态属性?
然后你可以使用:
但也许你需要更改名称空间前缀...
编辑:
问题出在因为你在另一个类中声明一个类......
我建议您将 Backgroud 类移到 OfficeColors 类之外并将其声明为静态,或者将 Background 类的属性移至 OfficeColors 类(可能带有 Background 前缀),或者在您尝试时使用命名空间。
玩得开心:)
编辑2:
使用 Nir 的方法,使用 + 号“aw:OfficeColors+Background.OfficeColor1”来引用嵌套类,我不知道:)
I'm asuming you have a Static property on the CustomClass?
Then you could use:
but maybe you need to change the namespace prefix...
EDIT:
The problem lies because you're declaring a class in another class...
I suggest you move the class Backgroud outside of the OfficeColors class and declare it static or move the Properties of the Background Class to the OfficeColors class (maybe with a Background prefix), OR use namespaces as you are kind of trying.
Have fun :)
EDIT2:
Use Nir's method using the + sign 'aw:OfficeColors+Background.OfficeColor1' to reference nested classes, didn't know that one :)
您必须将类的实例声明为资源之一。 (假设 CustomColorProperty 不是静态的)
You would have to declare an instance of the class as one of the resources. (Assuming CustomColorProperty is not static)