我有一个笔刷的确切名称(AliceBlue
, OrangeRed
等),我想知道是否有可能通过这个字符串选择笔刷。Brushes
是一个静态集合,我真的不知道如何做到这一点。我认为在正常的集合中,可以通过选择Linq
项目的name
属性来完成,但这里似乎不起作用。
使用系统中的BrushConverter。ComponentModel名称空间:
BrushConverter conv = new BrushConverter();
你可以使用一个颜色名称:
SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;
你也可以使用RGB值:
SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;
使用反射很容易做到:
// 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));
From BrushConverter
class (MSDN):
使用这个类将字符串转换为SolidColorBrush或一个ImageBrush。有关语法信息,请参阅这些类型页面。这门课是通常由解析器用于将属性字符串转换为笔刷。
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
您可以创建自己的StringToBrushConverter
,并通过传递字符串颜色变量并返回SolidColorBrush
变量,在其Convert
方法中使用上述代码。
我有两个解决方案。
一个使用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();
}
}