0

I have the exact name of a brush (AliceBlue, OrangeRed, etc) and I wonder if it is possible to select the brush by this string. Brushes is a static collection and I don't really know how to do this. I think in a normal collection it could be done by selecting the name property of an item with Linq, but doesn't seem to work here.

4

4 回答 4

9

使用 System.ComponentModel 命名空间中的 BrushConverter:

BrushConverter conv = new BrushConverter();

您可以使用颜色名称:

SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;

您还可以使用 RGB 值:

SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;
于 2013-06-21T10:46:11.693 回答
2

你可以很容易地通过反射来做到这一点:

// TODO: Validation :)
Brush brush = (Brush) typeof(Brushes).GetProperty(name)
                                     .GetValue(null);

或者,您可以使用一次反射来填充字典:

Dictionary<string, Brush> = 
    typeof(Brushes).GetProperties(BindingFlags.Public |
                                  BindingFlags.Static)
                   .ToDictionary(p => p.Name,
                                 p => (Brush) p.GetValue(null));
于 2013-06-21T10:42:19.390 回答
0

我有两个解决方案给你。

一个使用 SolidColorBrush 属性并将其设置为您想要的属性,如下所示。

另一种是使用带有BrushConverter类的转换。

<Window x:Class="WpfApplication1.DynamicSorting"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:local="clr-namespace:WpfApplication1"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DynamicSorting" Height="329" Width="610">
    <Window.Resources>
        <local:StringToBrushConverter x:Key="texttobrush"/>
    </Window.Resources>
    <Grid Background="{Binding SelectedBrush,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="242*" />
            <ColumnDefinition Width="346*" />
        </Grid.ColumnDefinitions>

        <ComboBox Height="35" SelectedItem="{Binding SelectedBrush,Mode=TwoWay}"  ItemsSource="{Binding AllBrushes}"  HorizontalAlignment="Left" Margin="25,32,0,0" x:Name="comboBox1" VerticalAlignment="Top" Width="143" />
    </Grid>
</Window>



 public partial class DynamicSorting : Window, INotifyPropertyChanged
    {
        public DynamicSorting()
        {
            InitializeComponent();
            if (FilesList == null)
                FilesList = new ObservableCollection<FileInfo>();

            var files = new System.IO.DirectoryInfo("C:\\Windows\\System32\\").GetFiles();
            foreach (var item in files)
            {
                FilesList.Add(item);
            }


            if (AllBrushes == null)
                AllBrushes = new ObservableCollection<string>();

            Type t = typeof(Brushes);

            var props = t.GetProperties();

            foreach (var item in props)
            {
                AllBrushes.Add(item.Name);
            }





            this.DataContext = this;



        }

        private SolidColorBrush _SelectedBrush;

        public SolidColorBrush SelectedBrush
        {
            get { return _SelectedBrush; }
            set
            {
                _SelectedBrush = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedBrush"));
            }

        }

        public ObservableCollection<FileInfo> FilesList { get; set; }

        public ObservableCollection<string> AllBrushes { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class StringToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Brushes.Transparent;

            var colortext = value.ToString();
            var BrushType = typeof(Brushes);
            var brush = BrushType.GetProperty(colortext);
            if (brush != null)
                return brush;

            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
于 2013-06-21T11:45:08.923 回答
0

BrushConverter类(MSDN):

使用此类将字符串转换为 SolidColorBrush 或 ImageBrush。有关语法信息,请参阅这些类型页面。解析器通常使用此类将属性字符串转换为画笔。

SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");

您可以通过传递字符串颜色变量和返回变量来创建自己的StringToBrushConverter代码并在其方法中使用上述代码。ConvertSolidColorBrush

于 2013-06-21T10:52:40.057 回答