How to set custom property as XAML value?
I have this library with custom Color开发者_JAVA百科 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.
Use this:
<SolidColorBrush Color="{x:Static aw:OfficeColors+Background.OfficeColor1}"/>
Notice the + sign instead of a dot to reference nested classes
I'm asuming you have a Static property on the CustomClass? Then you could use:
<SolidColorBrush Color="{x:Static eg:CustomClass.CustomColorProperty}"/>
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 :)
You would have to declare an instance of the class as one of the resources. (Assuming CustomColorProperty is not static)
<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>
精彩评论