title: Create a reference to Windows system color (a value type) with a converter in XAML
body: <p>I want to set up a theming system, and one of the theme should follow the Window's system colors. However because we have many more elements than there are for Windows system color, we have a need to adjust the shading of some system colors. Because colors are value types, it is difficult to substitute them with something that would allow us to use a converter in the place.</p>
  
  <p>For example, we have this in one of our theme XAML:</p>
  
  <pre><code>&lt;Color x:Key="ButtonFaceColor"&gt;#FFD3D3D3&lt;/Color&gt;
  </code></pre>
  
  <p>This is then referenced via the resource key in various parts of XAML, mostly with brushes but also with few other elements that accepts a color. For example, we might have a brush:</p>
  
  <pre><code>&lt;SolidColorBrush x:Key="ButtonFaceBrush" Color="{StaticResource ButtonFaceColor}"/&gt;
  </code></pre>
  
  <p>However, not only brushes use colors; we have other elements that might take the colors directly. For that reason, we want to customize using colors, rather than brushes.</p>
  
  <p>Now, we want to have a theme that follows the Windows' theme. If we just want to use system color as-is, we can change this into:</p>
  
  <pre><code>&lt;StaticResource x:Key="ButtonFaceColor" ResourceKey="{x:Static SystemColors.ControlColor}"/&gt;
  </code></pre>
  
  <p>However, we actually want to use a converter so that we can adjust the shading. So we actually want something more akin to this:</p>
  
  <pre><code>&lt;Binding x:Key="ButtonFaceColor" Source="{x:Static SystemColors.ControlColor}" Converter="{StaticResource RgbShadeConverter}" ConverterParameter="1"/&gt;
  </code></pre>
  
  <p>However, testing with even only the color reference as shown:</p>
  
  <pre><code>&lt;Binding x:Key="ButtonFaceColor" Source="{x:Static SystemColors.ControlColor}"/&gt;
  </code></pre>
  
  <p>We get an error which is pretty inscrutable. Judging from the errors, it's within the XAML trying to resolve the binding and possibly might not be liking that we are basically substituting a <code>Color</code> struct with a <code>Binding</code> object that <em>should</em> return a <code>Color</code> struct. </p>
  
  <pre><code>System.Windows.Markup.XamlParseException: 'Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '3' and line position '21'. ---&gt; System.Xaml.XamlObjectWriterException: 'Provide value on 'MS.Internal.Markup.StaticExtension' threw an exception.' Line number '105' and line position '22'. ---&gt; System.IO.FileNotFoundException: Could not load file or assembly 'Infralution.Localization.Wpf, PublicKeyToken=547ccae517a004b5' or one of its dependencies. The system cannot find the file specified.
  
     at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
  
     at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
  
     at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
  
     at System.Windows.Baml2006.Baml2006SchemaContext.ResolveAssembly(BamlAssembly bamlAssembly)
  
     at System.Windows.Baml2006.Baml2006SchemaContext.GetAssembly(Int16 assemblyId)
  
     at System.Windows.Baml2006.Baml2006SchemaContext.EnsureXmlnsAssembliesLoaded(String xamlNamespace)
  
     at System.Windows.Baml2006.Baml2006SchemaContext.GetXamlType(String xamlNamespace, String name, XamlType[] typeArguments)
  
     at MS.Internal.Xaml.XamlContext.GetXamlType(XamlTypeName typeName, Boolean returnUnknownTypesOnFailure, Boolean skipVisibilityCheck)
  
     at MS.Internal.Xaml.XamlContext.ResolveXamlType(String qName, Boolean skipVisibilityCheck)
  
     at MS.Internal.Xaml.Context.ObjectWriterContext.ServiceProvider_Resolve(String qName)
  
     at MS.Internal.Markup.StaticExtension.ProvideValue(IServiceProvider serviceProvider)
  
     at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
  
     --- End of inner exception stack trace ---
  
     at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
  
     at System.Xaml.XamlObjectWriter.Logic_ProvideValue(ObjectWriterContext ctx)
  
     at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
  
     at System.Xaml.XamlObjectWriter.WriteEndObject()
  
     at System.Xaml.XamlServices.Transform(XamlReader xamlReader, XamlWriter xamlWriter, Boolean closeWriter)
  
     at System.Windows.ResourceDictionary.EvaluateMarkupExtensionNodeList(XamlReader reader, IServiceProvider serviceProvider)
  
     at System.Windows.ResourceDictionary.GetKeyValue(KeyRecord key, IServiceProvider serviceProvider)
  
     at System.Windows.ResourceDictionary.SetKeys(IList`1 keyCollection, IServiceProvider serviceProvider)
  
     at System.Windows.Baml2006.WpfSharedBamlSchemaContext.&lt;&gt;c.&lt;Create_BamlProperty_ResourceDictionary_DeferrableContent&gt;b__297_0(Object target, Object value)
  
     at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
  
     --- End of inner exception stack trace ---
  
     at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
  
     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
  
     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
  
     at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
  </code></pre>
  
  <p>What is the magical incantation to get XAML to accept a reference with a converter on a value type in lieu of <code>Color</code> element? </p>
  
  <hr>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I also tried the following:</p>
  
  <pre><code>&lt;SolidColorBrush x:Key="ButtonFaceBrush" Color="{DynamicResource ButtonFaceColor}"/&gt;
  </code></pre>
  
  <p>This yields a design-time error:</p>
  
  <pre><code>An object of the type "System.Windows.Data.Binding" cannot be applied to a property that expects the type "System.Windows.Media.Color".
  </code></pre>
  
  <p>Which is kind of a clue to why <code>Binding</code> wouldn't have had worked, I guess. Changing it into:</p>
  
  <pre><code>&lt;SolidColorBrush x:Key="ButtonFaceBrush" Color="{Binding ButtonFaceColor}"/&gt;
  </code></pre>
  
  <p>dismisses the design-time error. However, actually running the application throws the same exception reported originally. </p>