2013-06-21 170 views
0

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

回答

8

使用来自System.ComponentModel命名空间BrushConverter:

BrushConverter conv = new BrushConverter(); 

你可以使用颜色的名称:

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

您也可以使用RGB值:

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

谢谢,我非常喜欢你的解决方案。 – Sturm

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)); 
0

BrushConverter类(MSDN):

使用这个类的字符串转换成SolidColorBrush或Imagerrush。查看这些类型页面的语法信息。这个类是 ,通常由解析器用于将属性字符串转换为画笔。

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

您可以创建自己的StringToBrushConverter并通过你的串色变量和返回SolidColorBrush变量在其Convert方法使用上面的代码。

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(); 
     } 
    }