Search code examples
wpfstaticbind

WPF Window Style Bind to static property of non-static class


I am trying to bind the border brush color to the management property of a Utilities file. What is the syntax to reference a static property of a static class that is in a non-static class?

<Window x:Class="Company.FieldServiceManagement.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FieldServiceManagement"
        xmlns:util="clr-namespace:Company.Utilities;assembly=Comp_Utilities"
        mc:Ignorable="d"
        Title="Field Service Management - Stand Alone" Height="750" MinHeight="500" Width="950" MinWidth="950">
    <Window.Resources>
        <util:ApplicationVariables x:Key="apv" />
    </Window.Resources>
    <Window.Style>
        <Style TargetType="{x:Type Window}">
            <Setter Property="BorderThickness" Value="3"/>
            <Setter Property="BorderBrush" Value="{Binding Source={StaticResource apv}, Path=Colors.Management}"/>
        </Style>
    </Window.Style>
    <Grid>
        <ContentControl x:Name="mainContentControl"/>
    </Grid>
</Window>

From the Utilities DLL assembly:

namespace Company.Utilities
{
    public class ApplicationVariables
    {
        public static class Colors
        {
            public static Color Employee { get; } = Color.FromArgb(192, 255, 192);
            public static Color Management { get; } = Color.FromArgb(255, 224, 192);
            public static Color HumanResources { get; } = Color.FromArgb(255, 192, 192);
            public static Color Payroll { get; } = Color.FromArgb(192, 255, 255);
            public static Color Rerpoting { get; } = Color.FromArgb(255, 192, 255);
        }
    }
}

Update

This difference, though seemingly minor to the outside viewpoint, is that the potential duplicate post is a simple structure of: non-static class -> static property while my situation is: non-static class -> static class -> static property. The referencing method is not as straightforward as the potential duplicate post.

Update # 2

With type issue identified by ASh, an extension was created to convert Drawing Color to Media Color and then a SolidColorBrush was created and referenced.

public static wpf.Color ColorDrawingToMedia(this Color color)
{
    return wpf.Color.FromArgb(color.A, color.R, color.G, color.B);
}
----
public static WPF.SolidColorBrush ManagementBrush = new WPF.SolidColorBrush(Management.ColorDrawingToMedia());
----
<Setter Property="BorderBrush" Value="{x:Static util:ApplicationVariables+Colors.ManagementBrush}"/>

Solution

  • You don't need an instance of any class to access static members.

    Also Colors properties don't seem to change, so instead of Binding you can use {x:Static} extension:

    <Setter Property="BorderBrush" Value="{x:Static util:ApplicationVariables+Colors.Management}"/>
    

    ApplicationVariables+Colors syntax means that Colors is a nested type inside ApplicationVariables


    BorderBrush expects value of type Brush, not Color. You can create SolidColorBrushes:

    public class ApplicationVariables
    {
        public static class Colors
        {
            public static Brush Employee { get; } = new SolidColorBrush(Color.FromArgb(192, 255, 192));
            public static Brush Management { get; } = new SolidColorBrush(Color.FromArgb(255, 224, 192));
            public static Brush HumanResources { get; } = new SolidColorBrush(Color.FromArgb(255, 192, 192));
            public static Brush Payroll { get; } = new SolidColorBrush(Color.FromArgb(192, 255, 255));
            public static Brush Rerpoting { get; } = new SolidColorBrush(Color.FromArgb(255, 192, 255));
        }
    }