Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's worth pointing out that the converter the other posts reference <em>already exists</em>, which is why you can do <code>&lt;Ellipse Fill="red"&gt;</code> in xaml in the first place. The converter is <code>System.Windows.Media.BrushConverter</code>:</p> <pre><code> BrushConverter bc = new BrushConverter(); Brush brush = (Brush) bc.ConvertFrom("Red"); </code></pre> <p>The more efficient way is to use the full syntax: </p> <pre><code>myEllipse.Fill = new SolidColorBrush(Colors.Red); </code></pre> <p><strong>EDIT</strong> in response to -1 and comments:</p> <p>The code above works perfectly fine <em>in code</em>, which is what the original question was asking about. You also <em>don't</em> want an <code>IValueConverter</code> - these are typically used for <em>binding</em> scenarios. A <code>TypeConverter</code> is the right solution here (because you're one-way converting a string to a brush). See <a href="http://bryantlikes.com/SilverlightIValueConverterVsTypeConverter.aspx" rel="noreferrer">this article</a> for details.</p> <p><strong>Further edit</strong> (having reread Aviad's comment): you don't need to explicitly use the <code>TypeConverter</code> in Xaml - it's used for you. If I write this in Xaml:</p> <pre><code>&lt;Ellipse Fill="red"&gt; </code></pre> <p>... then the runtime automagically uses a <code>BrushConverter</code> to turn the string literal into a brush. That Xaml is essentially converted into the equivalent longhand:</p> <pre><code>&lt;Ellipse&gt; &lt;Ellipse.Fill&gt; &lt;SolidColorBrush Color="#FFFF0000" /&gt; &lt;/Ellipse.Fill&gt; &lt;/Ellipse&gt; </code></pre> <p>So you're right - you can't use it in Xaml - but you don't need to.</p> <p>Even if you had a string value that you wanted to <em>bind</em> in as the fill, you don't need to specify the converter manually. This test from Kaxaml:</p> <pre><code>&lt;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"&gt; &lt;Page.Resources&gt; &lt;s:String x:Key="col"&gt;Red&lt;/s:String&gt; &lt;/Page.Resources&gt; &lt;StackPanel&gt; &lt;Ellipse Width="20" Height="20" Fill="{Binding Source={StaticResource col}}" /&gt; &lt;/StackPanel&gt; &lt;/Page&gt; </code></pre> <p>Strangely, you can't just use the <code>StaticResource col</code> and still have this work - but with the binding it and automatically uses the <code>ValueConverter</code> to turn the string into a brush.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload