Cannot add system color in styles in Silverlight?
Asked Answered
C

1

3

I defined resource in XAML for SystemColors. It is working great if I set Foregroung property directly to the TextBlock. However, I am getting an error shown below if I assign foreground property in style. I am not sure what is the issue and how to solve it. Any ideas are highly appreciated!

Code when I set foreground directly in the texblock. It is working perfectly

<TextBlock Text="WindowTextColor" Foreground="{Binding WindowTextColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}" />

Code when I set foreground property through the style. My app crashes:

<UserControl.Resources> 
  <local:ColorToBrushConverter x:Key="colorConverter" />
  <local:SystemColorsWrapper x:Key="SystemColors" />
  <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{Binding WindowColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}"/>
  </Style>    
</UserControl.Resources>

<Grid Background="#FFB8B8B8">
    <TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />      
</Grid>

The error I am getting:

System.Windows.Markup.XamlParseException occurred Message=Set property '' threw an exception. [Line: 11 Position: 41] LineNumber=11 LinePosition=41 StackTrace: at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at SilverlightSysColors.MainPage.InitializeComponent() at SilverlightSysColors.MainPage..ctor() InnerException: System.NotSupportedException Message=Cannot set read-only property ''. StackTrace: at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value) at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue) InnerException:

Confluent answered 28/4, 2011 at 9:9 Comment(0)
P
3

You can't include a binding in the a Setter. The binding is applied to the ValueProperty dependency property of the Setter which isn't really your intention. What happens is the style is applied during Xaml parsing (before the binding can apply its value) which in turn cause the style to be sealed. When an attempt by the binding is made to adjust the value of the Setter it fails because values become read only once the style has been used.

Edit

A work around based on my guess that dynamic binding isn't really that necessary you just want a convenient means of accessing the members of the SystemColors static class. My approach would be to create a derivative of ResourceDictionary to carry a complete set of resources to the SystemColors class including both a Color and a Brush for each property and named accordingly. A little bit of reflection is helpful:-

public class SystemColorsResources : ResourceDictionary
{
    public SystemColorsResources()
    {
        foreach (PropertyInfo pi in typeof(SystemColors).GetProperties())
        {
            Color c = (Color)pi.GetValue(null, null);
            Add(pi.Name, c);
            Add(pi.Name.Replace("Color", "Brush"), new SolidColorBrush(c));
        }
    }
}

With this class in your app include an instance of it in your MergedDictionaries list in the App.Xaml:-

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <local:SystemColorsResources />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Now you can use the system color property names with their "Brush" variants as straight forward static resources:-

<UserControl.Resources>
  <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource WindowBrush}" />
  </Style>
</UserControl.Resources>

<Grid Background="#FFB8B8B8">
    <TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />
</Grid>
Polito answered 28/4, 2011 at 9:41 Comment(5)
It is the best work around I could find. Thank you a lot for sharing with his technique. I highly appreaciate it.Confluent
I have a question. I see that you replace Color with Brush. I am wondering if I need to apply the color to border backgroung then it will not work. Is any way to to be able to use it for backgroung color too?Confluent
@vladc77: I don't know of any control properties that take a Color instead of a Brush. You should be using the "xxxxBrush" resource key name for a Background property since it will be expecting a Brush. Note for belts and braces the SystemColorResources contains both Colors and Brushes. You might want to use the Colors when creating your own brushes based on system colors however I doubt you'll find any other use for them.Polito
Thank you for getting back to me. I tried to apply background color to the border control using this technique but the color was not changed. I replaced foregroung to background property in the setter. Any ideas why it is not working. Thank you again.Confluent
@vladc: Without seeing what you are actually doing its difficult to say. It worries me that you are still using the word "color" instead of "brush". It would help to know that you understand that distinction.Polito

© 2022 - 2024 — McMap. All rights reserved.