开发者

Cannot add system color in styles in Silverlight?

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 Wind开发者_JAVA技巧owColor, 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:


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜