WPF Window Style Bind to static 属性 of non-static class
WPF Window Style Bind to static property of non-static class
我正在尝试将边框画笔颜色绑定到 Utilities 文件的管理 属性。引用非静态 class 中静态 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>
来自实用程序 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);
}
}
}
更新
这种差异,虽然从外部观点来看似乎微不足道,但潜在的重复项 post 是一个简单的结构:非静态 class -> 静态 属性 而我的情况是:非静态class -> 静态class -> 静态属性。引用方法不像潜在的重复方法那样简单 post.
更新#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}"/>
您不需要任何 class 的实例来访问 static
成员。
此外 Colors
属性似乎没有改变,因此您可以使用 {x:Static}
扩展名代替 Binding
:
<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));
}
}
我正在尝试将边框画笔颜色绑定到 Utilities 文件的管理 属性。引用非静态 class 中静态 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>
来自实用程序 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);
}
}
}
更新
这种差异,虽然从外部观点来看似乎微不足道,但潜在的重复项 post 是一个简单的结构:非静态 class -> 静态 属性 而我的情况是:非静态class -> 静态class -> 静态属性。引用方法不像潜在的重复方法那样简单 post.
更新#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}"/>
您不需要任何 class 的实例来访问 static
成员。
此外 Colors
属性似乎没有改变,因此您可以使用 {x:Static}
扩展名代替 Binding
:
<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));
}
}