首页 > 解决方案 > WPF 窗口样式绑定到非静态类的静态属性

问题描述

我正在尝试将边框画笔颜色绑定到实用程序文件的管理属性。引用非静态类中静态类的静态属性的语法是什么?

<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>

从实用程序 DLL 程序集:

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);
        }
    }
}

更新

这种差异虽然在外界看来似乎很小,但潜在的重复帖子是一个简单的结构:非静态类 -> 静态属性,而我的情况是:非静态类 -> 静态类 -> 静态属性。引用方法不像潜在的重复帖子那样简单。

更新#2

由于 ASh 确定了类型问题,因此创建了一个扩展以将绘图颜色转换为媒体颜色,然后创建并引用了 SolidColorBrush。

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}"/>

标签: wpfstaticbind

解决方案


您不需要任何类的实例来访问static成员。

Colors属性似乎也没有改变,因此您Binding可以使用{x:Static}扩展名代替:

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

ApplicationVariables+Colors语法意味着 Colors 是 ApplicationVariables 中的嵌套类型


BorderBrush期望 Brush 类型的值,而不是 Color。您可以创建 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));
    }
}

推荐阅读