Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update</strong><br> Did some more thinking about this problem. You can just bind the value :) This will both validate the color and set it. An invalid value will use the FallbackValue White.</p> <pre><code>&lt;Rectangle Fill="{Binding ElementName=textBox, Path=Text, FallbackValue=White}"/&gt; &lt;TextBox Name="textBox" Text="Green"/&gt; </code></pre> <p>And if you need to have methods like IsColorValid and GetColorFromValue in code behind you can use Bindings there as well like this</p> <pre><code>private bool IsValidColor(string colorValue) { if (colorValue == "White" || colorValue == "#FFF" || colorValue == "#FFFF" || colorValue == "#FFFFFF" || colorValue == "#FFFFFFFF") { return true; } Brush fallbackBrush = new SolidColorBrush(Colors.White); Rectangle validationRectangle = new Rectangle(); Binding validationBinding = new Binding(); validationBinding.Source = colorValue; validationBinding.FallbackValue = fallbackBrush; validationRectangle.SetBinding(Rectangle.FillProperty, validationBinding); if (validationRectangle.Fill == fallbackBrush) { return false; } return true; } private Color GetColorFromValue(string colorValue) { Rectangle validationRectangle = new Rectangle(); Binding validationBinding = new Binding(); validationBinding.Source = colorValue; validationBinding.FallbackValue = new SolidColorBrush(Colors.White); validationRectangle.SetBinding(Rectangle.FillProperty, validationBinding); return ((SolidColorBrush)validationRectangle.Fill).Color; } </code></pre>
 

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