如何使用.Foreground?

发布于 2024-10-04 10:21:59 字数 261 浏览 3 评论 0原文

我有一个 silverlight 应用程序,在应用程序中的某处包含以下几行:

txtMelding.Foreground = new SolidColorBrush(Colors.Black);

稍后

txtMelding.Foreground = new SolidColorBrush(Colors.Gray);

我想要一个 IF 来检查前景色是否为黑色或灰色。如何?

I have an silverlight application with the to following lines somewhere in the app:

txtMelding.Foreground = new SolidColorBrush(Colors.Black);

and

txtMelding.Foreground = new SolidColorBrush(Colors.Gray);

Later I want to have an IF that checks whenever the foreground color is black or gray. How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随波逐流 2024-10-11 10:21:59

您需要将前景画笔转换为 SolidBrush - 假设您知道它始终是实心画笔:

SolidBrush brush = (SolidBrush) txtMelding.Foreground;
if (brush.Color == Colors.Gray)
{
    ...
}

如果您不知道它将始终是实心画笔SolidBrush,您可以使用as

SolidBrush brush = txtMelding.Foreground as SolidBrush;
if (brush != null && brush.Color == Colors.Gray)

另一方面,颜色不应该反映您状态的某些方面吗? (从设计角度来说)对此做出反应可能比对 UI 外观做出反应更好。

You'll need to cast the foreground brush to SolidBrush - assuming you know it always will be a solid brush:

SolidBrush brush = (SolidBrush) txtMelding.Foreground;
if (brush.Color == Colors.Gray)
{
    ...
}

If you don't know that it'll always be a SolidBrush, you can use as:

SolidBrush brush = txtMelding.Foreground as SolidBrush;
if (brush != null && brush.Color == Colors.Gray)

On the other hand, shouldn't the colour be reflecting some aspect of your state? It may well be better (in design terms) to react to that than reacting to the UI appearance.

调妓 2024-10-11 10:21:59
if (((SolidBrush)txtMelding.Foreground).Color == Colors.Gray)
{
    // the color is gray
}
if (((SolidBrush)txtMelding.Foreground).Color == Colors.Gray)
{
    // the color is gray
}
月野兔 2024-10-11 10:21:59

我发现如何做到这一点:

if (((SolidColorBrush)txtMelding.Foreground).Color == Colors.Gray)

I found out how to do it:

if (((SolidColorBrush)txtMelding.Foreground).Color == Colors.Gray)
§普罗旺斯的薰衣草 2024-10-11 10:21:59

设置和比较 SolidColorBrushes 的另一种更简洁的方法是使用内置 画笔,位于 System.Windows.Media 中:

txtMelding.Foreground = Brushes.Black;

if(txtMelding.Foreground == Brushes.Black)

我看来更好;)

Another and more cleaner way to set and compare SolidColorBrushes is to use the buildin Brushes which lives in System.Windows.Media:

txtMelding.Foreground = Brushes.Black;

and

if(txtMelding.Foreground == Brushes.Black)

Much better imo ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文