如何设置按钮的样式?我使用xceed.wpf.toolkit
Xceed.Wpf.Toolkit.MessageBox mbox = new Xceed.Wpf.Toolkit.MessageBox();
System.Windows.Style style = new System.Windows.Style(typeof(System.Windows.Controls.Button));
style.Setters.Add( new System.Windows.Setter(System.Windows.Controls.Button.ForegroundProperty, Brushes.DarkGreen));
mbox.OkButtonStyle = style;
我得到错误
System.Windows.Markup.XamlParseException: ''System.Drawing.SolidBrush' is not a valid value for the 'System.Windows.Documents.TextElement.Foreground' property on a Setter.'
确保使用WPF库,而不是WindowsForms或GDI 一个...
您应该使用的内容:System.Windows.Media.Brushes
包含DarkGreen
为System.Windows.Media.SolidColorBrush
(在presentationCore.dll中(。
您当前使用的是System.Drawing.Brushes
和System.Drawing.SolidBrush
。
TextElement.Foreground
是类型System.Windows.Media.Brush
。那就是"数据类型"。您必须为其分配该类型的值或该类型的某个子类。
System.Drawing.Brushes.DarkGreen
是System.Drawing.Brush
类型,它不是System.Windows.Media.Brushes
的子类。那是来自Windows表单之类的东西,而不是WPF。您需要使用WPF刷子对象进行WPF控件。
在C#文件的顶部摆脱using System.Drawing;
。在WPF项目中,这只会给您带来麻烦。改用System.Windows.Media.Brushes.DarkGreen
。
style.Setters.Add( new System.Windows.Setter(System.Windows.Controls.Button.ForegroundProperty,
System.Windows.Media.Brushes.DarkGreen));
您还可以将样式创建为XAML资源,并使用FindResource()
加载。然后,您只会说" darkgreen",然后让XAML解析器担心要创建哪种刷子:
<Style
x:Key="XCeedMBoxButtonStyle"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}"
>
<Setter Property="TextElement.Foreground" Value="DarkGreen" />
</Style>
c#
var style = FindResource("XCeedMBoxButtonStyle") as Style;
,但是随后您不得不担心在可以找到它的某个地方定义它,如果您只使用正确的刷子类,您的工作就可以了。
在多个.NET名称空间中,我们有多个名为Brush
的类,它具有非信息名称,例如" system.windows.media" vs" vs" system.drawing"(,但不幸的是,这一切都会以这种方式增长。