【发布时间】:2011-11-16 18:32:59
【问题描述】:
定义要在 .cs 和 .xaml 文件中使用的对象的正确方法是什么?例如,我在“constants.cs”类中定义了自定义颜色和画笔:
using System.Windows.Media;
namespace MyProject
{
public static class Constants
{
public static Color MyBlue = Color.FromArgb(255, 35, 97, 146);
public static SolidColorBrush MyBlueBrush = new SolidColorBrush(MyBlue);
}
}
我想在 .xaml 或 .cs 文件中使用 MyBlue 或 MyBlueBrush。
我可以像这样获得 .cs 文件中的颜色:
namespace MyProject
{
public partial class MyColorWindow : Window
{
public MyColorWindow()
{
InitializeComponent();
btnOne.Background = Constants.MyBlueBrush;
}
}
}
但是如何在 XAML 文件中找到它?例如,我应该在下面的代码中添加什么来访问 MyBlueBrush?
<Window x:Class="MyProject.MyColorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyColorWindow" Height="300" Width="300">
<Grid>
<Button Name="btnOne" Background="Purple" Margin="0,32,0,185" />
<Button Name="btnTwo" Background="Orange" Margin="0,132,0,85" /> <!-- I want this background to be MyBlueBrush too -->
</Grid>
</Window>
【问题讨论】: