[英]Using C# variable to set XAML attributes
I have been trying to use C# variables to set the values of XAML attributes, but haven't had success.我一直在尝试使用 C# 变量来设置 XAML 属性的值,但没有成功。 I have a variable holding the value
"Verdana"
, the font I'd like to use on the buttons of the application, however when trying to compile the code I get an exception:我有一个变量保存值
"Verdana"
,这是我想在应用程序按钮上使用的字体,但是在尝试编译代码时出现异常:
"ArgumentException: "Verdana" is not a valid value for the attribute "FontFamily".
“ArgumentException:“Verdana”不是属性“FontFamily”的有效值。
The same exception appears for every attribute with a value coming from a variable, besides width and height.除了宽度和高度之外,每个具有来自变量的值的属性都会出现相同的异常。 I'm grateful for any suggestions on how to solve this.
我很感激关于如何解决这个问题的任何建议。
I used the following code:我使用了以下代码:
C# Class C# Class
namespace Ueb21d_SplitPanel
{
public static class AzoConst
{
public static readonly double FONTSIZE_BUTTON = 16;
public static readonly string BACKGROUND_BUTTON = "Green";
public static readonly string FONT_BUTTON = "Verdana";
public static readonly string FONTCOLOR_BUTTON = "White";
public static readonly double WIDTH_BUTTON = 70;
public static readonly double HEIGHT_BUTTON = 30;
}
}
XML Namespace XML 命名空间
xmlns:local="clr-namespace:Ueb21d_SplitPanel;assembly=Ueb21d_SplitPanel"
XAML code for one button XAML一键码
<Button x:Name="btnClose" Content="Close"
Width="{x:Static local:AzoConst.WIDTH_BUTTON}"
Height="{x:Static local:AzoConst.HEIGHT_BUTTON}" Margin="5,0,0,0"
IsCancel="True" RenderTransformOrigin="0.5,0.5"
FontFamily="{x:Static local:AzoConst.FONT_BUTTON}"
Foreground="{x:Static local:AzoConst.FONTCOLOR_BUTTON}"
Background="{x:Static local:AzoConst.BACKGROUND_BUTTON}"
FontSize="{x:Static local:AzoConst.FONTSIZE_BUTTON}"/>
Use the appropriate types that match the types of the dependency properties and it will work.使用与依赖属性类型相匹配的适当类型,它将起作用。
FontFamily
is of type FontFamily
FontFamily
属于FontFamily
类型Foreground
is of type Brush
Foreground
是Brush
类型Background
is of type Brush
Background
类型为Brush
public static class AzoConst
{
public static readonly double FONTSIZE_BUTTON = 16;
public static readonly Brush BACKGROUND_BUTTON = Brushes.Green;
public static readonly FontFamily FONT_BUTTON = new FontFamily("Verdana");
public static readonly Brush FONTCOLOR_BUTTON = Brushes.White;
public static readonly double WIDTH_BUTTON = 70;
public static readonly double HEIGHT_BUTTON = 30;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.