wpf - 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}"/>
解决方案
您不需要任何类的实例来访问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));
}
}
推荐阅读
- postgresql - 创建一个序列实际上是创建一个大序列
- mysql - 清理 Rails 迁移和持久化生产数据
- local - 使用 LINQPad 5 + TX (LINQ to Traces) 使用服务结构本地集群 ETW 事件的指南?
- java - 如何手动取消/处置 RXJava2 Flowable?
- wordpress - Wordpress 的索引页面不是 SSL 安全的
- node.js - npm 脚本使用哈希开始生产构建
- android - 你如何在android中获得一个switch语句来识别一个复选框?
- java - 如何对依赖于其他 Util 类方法的 Util 类进行单元测试?
- c# - 如何向前端显示数据?
- python - 获取“UnboundLocalError:分配前引用的局部变量'err'”