WPF snap to change ellipse fill color

Probably a simple question, but:

How to programmatically change the color of an ellipse that is defined in XAML based on a variable?

Everything I read about binding is based on collections and lists - can I set it simply (and literally) based on the value of a string variable? string color = "red" color = "# FF0000"

+9
c # wpf binding xaml ellipse
source share
3 answers

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.

+17
source share

what you need to do is implement your own converter to convert colors to a brush object. Something like that...

 public class ColorToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { System.Drawing.Color col = (System.Drawing.Color)value; Color c = Color.FromArgb(col.A, col.R, col.G, col.B); return new System.Windows.Media.SolidColorBrush(c); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { SolidColorBrush c = (SolidColorBrush)value; System.Drawing.Color col = System.Drawing.Color.FromArgb(c.Color.A, c.Color.R, c.Color.G, c.Color.B); return col; } } 

And then specify this converter in your binding

 Fill = "{Binding Colors.Red, Converter = {StaticResource ColorToBrushConverter}"
+6
source share

using

 System.Windows.Media 

If your ellipse name in your XAML is my_ellipse ,
write something like this:

 my_ellipse.Fill = System.Windows.Media.Brushes.Red; 

or that:

 my_ellipse.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#F4F4F5") 
+2
source share

All Articles