c# - 选择一个随机画笔

标签 c# .net wpf brushes

我正在寻找一种在 Brushes 集合(Aqua、Azure、...Black、...)中随机选择 Brush 的方法。有什么线索吗?

最佳答案

你可以使用一些反射,像这样:

private Brush PickBrush()
{
    Brush result = Brushes.Transparent;

    Random rnd = new Random();

    Type brushesType = typeof(Brushes);

    PropertyInfo[] properties = brushesType.GetProperties();

    int random = rnd.Next(properties.Length);
    result = (Brush)properties[random].GetValue(null, null);

    return result;
}

这样就可以了。您可能希望将随机化更改为使用外部 Random 实例,而不是像我的示例那样在每次调用该方法时都重新创建一个新种子。

关于c# - 选择一个随机画笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084398/

相关文章:

.net - 数据集中的可空 Int 列

c# - 如何从 .cs 文件更改 WPF 菜单项中的文本?

wpf - 是否可以像在 WPF 中扩展样式一样扩展 ControlTemplate?

.net - 为什么我应该使用 WPF 而不是 Winforms? WPF 优于 Winforms 的任何示例?

c# - wpf制作幻灯片动画

c# - 在 Razor 中,如何根据元素的属性检查 ViewBag IEnumerable 对象中是否存在对象?

c# - 在 ASP.NET 5 中使用 System.IdentityModel.Tokens.Jwt 编码 JWT token

c# - Show 和 ShowDialog 将不起作用

c# - 如何在 C# 中使用 Pascal Casing 和 Camel Casing 作为缩写词?

mysql - 访问 mysql 数据库并将其显示在 gridview 或表中