'Label'不包含'ForeColor'的定义。

时间:2021-07-16 16:54:47

Basically, I have a button, that, when pressed, should change another label's text color. However, whenever I try to run it, I get the error:

基本上,我有一个按钮,按下时,应该更改另一个标签的文本颜色。但是,每当我尝试运行它时,我都会收到错误:

'Label' does not contain a definition for 'ForeColor' and no extension method 'ForeColor' accepting a first argument 'Label' could be found (are you missing a using directive or an assembly reference?)

'Label'不包含'ForeColor'的定义,并且没有扩展方法'ForeColor'接受第一个参数'Label'(你是否缺少using指令或汇编引用?)

The basic code is:

基本代码是:

private void Button_Click(object sender, RoutedEventArgs e)
{
    tlabel.ForeColor = System.Drawing.Color.Red;
}

What should I do?

我该怎么办?

Sorry if the question might be easily fixable but I just recently started with C# and I couldn't find any solutions that fix my problem (or questions that are even similar).

很抱歉,如果问题可能很容易解决,但我最近刚开始使用C#,我找不到任何可以解决我的问题的解决方案(或者甚至类似的问题)。

1 个解决方案

#1


1  

The property is called Foreground and it's a Brush, not a Color.

该属性称为前景,它是一个画笔,而不是一个颜色。

tlabel.Foreground = System.Windows.Media.Brushes.Red;

The advantage of using a brush instead of a color is that red is just red, but a Brush could be a lot of things. The system brush I showed you is a SolidColorBrush -- just red -- but there are various gradient brushes, ImageBrushes, and so on.

使用画笔而不是颜色的优点是红色只是红色,但画笔可能是很多东西。我给你看的系统画笔是一个SolidColorBrush - 只是红色 - 但有各种渐变画笔,ImageBrushes等。

System.Drawing is a windows forms namespace, not WPF. ForeColor is a windows forms property as well. Make sure the documentation you're looking at is for WPF, not windows forms.

System.Drawing是一个Windows窗体命名空间,而不是WPF。 ForeColor也是一个Windows窗体属性。确保您正在查看的文档适用于WPF,而不是Windows窗体。

#1


1  

The property is called Foreground and it's a Brush, not a Color.

该属性称为前景,它是一个画笔,而不是一个颜色。

tlabel.Foreground = System.Windows.Media.Brushes.Red;

The advantage of using a brush instead of a color is that red is just red, but a Brush could be a lot of things. The system brush I showed you is a SolidColorBrush -- just red -- but there are various gradient brushes, ImageBrushes, and so on.

使用画笔而不是颜色的优点是红色只是红色,但画笔可能是很多东西。我给你看的系统画笔是一个SolidColorBrush - 只是红色 - 但有各种渐变画笔,ImageBrushes等。

System.Drawing is a windows forms namespace, not WPF. ForeColor is a windows forms property as well. Make sure the documentation you're looking at is for WPF, not windows forms.

System.Drawing是一个Windows窗体命名空间,而不是WPF。 ForeColor也是一个Windows窗体属性。确保您正在查看的文档适用于WPF,而不是Windows窗体。