选择按名称刷

时间:2013-06-21 10:40:45

标签: c# wpf brush

我有一个画笔的确切名称(AliceBlueOrangeRed等),我想知道是否可以通过此字符串选择画笔。 Brushes是一个静态集合,我真的不知道如何做到这一点。我认为在正常的集合中,可以通过选择name项目的Linq属性来完成,但似乎不起作用。

4 个答案:

答案 0 :(得分:7)

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

BrushConverter conv = new BrushConverter();

您可以使用颜色名称:

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

您还可以使用RGB值:

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

答案 1 :(得分: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));

答案 2 :(得分:0)

来自BrushConverter班级(MSDN):

  

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

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

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

答案 3 :(得分:0)

我有两个解决方案。

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

另一个是使用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();
        }
    }