我有笔刷的确切名称(AliceBlue
,OrangeRed
等),我在想是否可以通过此字符串选择笔刷。 Brushes
是一个静态集合,我不知道如何做到这一点。我认为在普通收藏中,可以通过选择Linq
的物品name
属性来完成,但在这里似乎不起作用。按名称选择笔刷
Q
按名称选择笔刷
0
A
回答
8
使用来自System.ComponentModel命名空间BrushConverter:
BrushConverter conv = new BrushConverter();
你可以使用颜色的名称:
SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;
您也可以使用RGB值:
SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;
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();
}
}
相关问题
- 1. d3画布笔刷/选择
- 2. jQuery选择按名称
- 3. $ _POST按名称选择textarea?
- 4. jQuery选择:按名称
- 5. simple_form选择:按名称
- 6. Jquery按名称选择按钮
- 7. 框图中的笔刷选择(d3.js)
- 8. 按名称选择Collada组和儿童
- 9. 按名称在div中选择输入
- 10. 是否有按名称选择元素?
- 11. 按名称选择从数据帧
- 12. xsltproc不会按名称选择元素
- 13. 如何按名称选择TreeNode?
- 14. 如何按名称选择Outlook日历?
- 15. SQL按列名称列表选择/组
- 16. 按重要性选择列名称
- 17. 按名称和ID(ELEVATEDB)订购选择
- 18. 选择按名称或ID,而不是
- 19. MYSQL按名称在PHP中选择行
- 20. 在jquery中按名称选择元素
- 21. 按jQuery名称选择输入
- 22. Filemaker XSL按名称选择列
- 23. 按名称选择器,与我的类名称框中jQuery
- 24. jQuery选择名称
- 25. 选择* vs选择所有列名称
- 26. 如何按颜色选择,然后选择笔画?
- 27. 选择并刷新不知道ID或名称的iframe
- 28. 按属性/用户按用户名称选择实体
- 29. 复制的选择名称
- 30. ReGEX只选择TVShow名称
谢谢,我非常喜欢你的解决方案。 – Sturm