It is worth noting that the converter contains a link to other messages, so you can do <Ellipse Fill="red"> in xaml in the first place. Converter System.Windows.Media.BrushConverter :
BrushConverter bc = new BrushConverter(); Brush brush = (Brush) bc.ConvertFrom("Red");
A more efficient way is to use the full syntax:
myEllipse.Fill = new SolidColorBrush(Colors.Red);
EDIT in response to -1 and comments:
The code above works fine in the code, as the original question asked. You also do not want IValueConverter - they are usually used to bind scripts. A TypeConverter is the right solution here (because you are converting a string to a brush one-way). See this article for more details.
Further editing (with re-reading the Aviad comment): you do not need to explicitly use TypeConverter in Xaml - it is used for you. If I write this in Xaml:
<Ellipse Fill="red">
... then at runtime, the BrushConverter used BrushConverter to turn the string literal into a brush. This Xaml essentially translates to the equivalent longhand:
<Ellipse> <Ellipse.Fill> <SolidColorBrush Color="#FFFF0000" /> </Ellipse.Fill> </Ellipse>
So, you are right - you cannot use it in Xaml, but you do not need it.
Even if you have a string value that you want to associate as padding, you do not need to specify the converter manually. This test is from Kaxaml:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"> <Page.Resources> <s:String x:Key="col">Red</s:String> </Page.Resources> <StackPanel> <Ellipse Width="20" Height="20" Fill="{Binding Source={StaticResource col}}" /> </StackPanel> </Page>
Strange, you can't just use StaticResource col and still have this work, but with binding and automatically use ValueConverter to turn the string into a brush.
Dan puzey
source share