0

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}"/>
Galactic
  • 400
  • 4
  • 14
  • I tried that originally before posting along with many other scenarios. With no luck and tired of banging my head, I created the post rather then hijacking another thread via comments. The issue seems more with the SolidColorBrush type vs. using Color. – Galactic Oct 08 '19 at 17:53
  • There's a wealth of information in the proposed duplicate, and there's nothing in your question that suggests you actually tried everything. Binding to static properties isn't hard. Have you tried `{x:Static}` as in [this answer](https://stackoverflow.com/a/936331)? Without a good [mcve], there's no way to know what you've done wrong. – Peter Duniho Oct 08 '19 at 18:07
  • And the difference you note in your question is not a difference at all. All properties exist in _some_ class, and binding a static property does not depend on which class, nor whether that class is static or not. The "referencing method" is exactly as straightforward as in the proposed duplicate. – Peter Duniho Oct 08 '19 at 18:09
  • The syntax for nested static referencing is not the same as the "+" comes in to play. And yes, I tried the Source/Path referencing. One should not assume that just because a poster doesn't say "I tried everything out there" that they have not already done a bunch of research and tried many of the methods you are linking. Yes, I have seen and tried these. My last step for 20+ years has been to post after exhaustive research, not first thing. – Galactic Oct 08 '19 at 18:13
  • Possible duplicate of [Bind to property in a nested static class](https://stackoverflow.com/questions/11097347/bind-to-property-in-a-nested-static-class) – Peter Duniho Oct 08 '19 at 18:18
  • Possible duplicate of [Using {x:Static} to access property of static field in a static class](https://stackoverflow.com/questions/42391472/using-xstatic-to-access-property-of-static-field-in-a-static-class) – Peter Duniho Oct 08 '19 at 18:19
  • For the minimal reproducible example...the code was posted. It is a simple window with a content control. Combined with a DLL that contains corporate color themes. – Galactic Oct 08 '19 at 18:20
  • 1
    _"One should not assume that just because a poster doesn't say "I tried everything out there" that they have not already done a bunch of research"_ -- if you'd ever spent any significant time answering other people's questions on Stack Overflow, you'd know that's exactly wrong. 99.94% of the time, the author of the question did in fact not do _any_ search, never mind any sort of thorough research that would justify them posting a question. While the direct static property question might not apply here, I still submit you are not the first person to run into this nor ask a question about it. – Peter Duniho Oct 08 '19 at 18:21
  • The nested static class example shows all layers that are static and rather then the top layer being non-static. Tried that previously during research. – Galactic Oct 08 '19 at 18:22
  • You posted code, but it doesn't compile. Either you're using the wrong `Color` type (i.e. from `Drawing`, where there's a three-parameter overload of `FromArgb()`, or you didn't provide the actual initialization of your static properties (because the `Color` type in the `Media` namespace requires four arguments for the `FromArgb()` method) – Peter Duniho Oct 08 '19 at 18:26
  • Whether the outer class is static or not won't matter. The type relationships don't depend on instances. – Peter Duniho Oct 08 '19 at 18:26
  • Not being the first person to ask that question is a broad statement that applies to virtually ever question. Just because someone may have asked the question does not mean the title of the post was or was not relevant the actual problem. Additionally, with so much information available, it is reasonable these days that relevant posts can easily be overlooked. – Galactic Oct 08 '19 at 18:26
  • @PeterDuniho If you look at the response below by ASh you will see that the type issue is what they first concluded. This is my current focus trying to this working. – Galactic Oct 08 '19 at 18:37
  • I tested your code with syntax explained [in this answer](https://stackoverflow.com/a/11098101) and it works fine, once you fix the bug that you're binding a `Color` to a property that expects a `Brush`. The search that turned up that answer is https://stackoverflow.com/search?q=%5Bwpf%5D+bind+nested+static+property. That is hardly an esoteric set of keywords. It's the search you should have tried _first_. – Peter Duniho Oct 08 '19 at 18:37
  • As for that other bug you have: ` ` fixes that (i.e. create a brush and set the color on that, instead of the property that isn't a color property) – Peter Duniho Oct 08 '19 at 18:44

1 Answers1

0

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));
    }
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • Thanks. That is taking results in the right direction. It seems Colors is now recognized with a message reading: The member "Management" is not recognized or is not accessible. – Galactic Oct 08 '19 at 15:35
  • @Galactic, I'm getting "Invalid member type: expeted type 'Brush', actual type is 'Color'". see my edit – ASh Oct 08 '19 at 15:45