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.

有帮助吗?

解决方案

Use BrushConverter from the System.ComponentModel namespace:

BrushConverter conv = new BrushConverter();

You can use a color name:

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

You can also use an RGB value:

SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;

其他提示

You could do it with reflection easily enough:

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

Alternatively, you could use reflection once to populate a dictionary:

Dictionary<string, Brush> = 
    typeof(Brushes).GetProperties(BindingFlags.Public |
                                  BindingFlags.Static)
                   .ToDictionary(p => p.Name,
                                 p => (Brush) p.GetValue(null));

From BrushConverter class (MSDN):

Use this class to convert a string into a SolidColorBrush or an ImageBrush. See these type pages for syntax information. This class is typically used by the parser to convert attribute strings to brushes.

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

You can create your own StringToBrushConverter and use above code in its Convert method by passing your string color variables and returning SolidColorBrush variables.

I have two solution for you.

one use A SolidColorBrush property and set it with your desire property like below.

And other one is use convert with BrushConverter Class.

<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();
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top