none
Brush based on another brush.

    Question

  • I want to create a brush with key 'WindowBgBrush' in my application-scope resource dictionary to have a possibility to change themes (I have several such dictionaries). One theme should be system, that is why I want this brush to 'derive' in any way from SystemColors.WindowBrush in this theme. In another themes the brush can be declared as my own LinearGradientBrush, SolidColorBrush, etc.

    Is there any possibilities for a brush inheritance in WPF? Or can I achieve my goal in any another way?
    Tuesday, September 23, 2008 12:23 PM

Answers

  • You can't create a custom Brush, but it is completely not necessary in this situation.
    All you have to do is to add the static brush as a value into the ResourceDictionary with the required key, using the XAML Static markup extension:

    <x:Static x:Key="WindowBgBrush" Member="SystemColors.ActiveCaptionBrush" /> 

    Note that the preferred way to restyle the UI is by using styles. Instead of replacing colors you should consider of replacing styles:

    <Application.Resources> 
         
        <!-- The default syle --> 
        <Style x:Key="DefaultWindowStyle" TargetType="{x:Type Window}"
            <Setter Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" /> 
        </Style> 
         
        <Style x:Key="NonDefaultWindowStyle" TargetType="{x:Type Window}"
            <Setter Property="Background" Value="AliceBlue" /> 
        </Style> 
         
    </Application.Resources> 
     

    Regards,
    Tomer



    Tomer Shamam | .NET Technologies Expert | Smart Client Leader | Sela Group
    • Proposed as answer by תומר שמם Tuesday, September 23, 2008 8:48 PM
    • Edited by תומר שמם Tuesday, September 23, 2008 8:49 PM
    • Marked as answer by Marco Zhou Monday, September 29, 2008 10:36 AM
    Tuesday, September 23, 2008 8:48 PM

All replies

  • You can't create a custom Brush, but it is completely not necessary in this situation.
    All you have to do is to add the static brush as a value into the ResourceDictionary with the required key, using the XAML Static markup extension:

    <x:Static x:Key="WindowBgBrush" Member="SystemColors.ActiveCaptionBrush" /> 

    Note that the preferred way to restyle the UI is by using styles. Instead of replacing colors you should consider of replacing styles:

    <Application.Resources> 
         
        <!-- The default syle --> 
        <Style x:Key="DefaultWindowStyle" TargetType="{x:Type Window}"
            <Setter Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" /> 
        </Style> 
         
        <Style x:Key="NonDefaultWindowStyle" TargetType="{x:Type Window}"
            <Setter Property="Background" Value="AliceBlue" /> 
        </Style> 
         
    </Application.Resources> 
     

    Regards,
    Tomer



    Tomer Shamam | .NET Technologies Expert | Smart Client Leader | Sela Group
    • Proposed as answer by תומר שמם Tuesday, September 23, 2008 8:48 PM
    • Edited by תומר שמם Tuesday, September 23, 2008 8:49 PM
    • Marked as answer by Marco Zhou Monday, September 29, 2008 10:36 AM
    Tuesday, September 23, 2008 8:48 PM
  • I tried your advise but I have exception when try to use the brush in code, e.g.
    Background="{DynamicResource WindowBackgroundBrush}" 
    I have a 'System.Windows.Markup.StaticExtension' is not a valid value for property 'Background' exception.
    Background property has a Brush type.
    Wednesday, September 24, 2008 7:48 AM
  • Make sure that the "SystemColors.XXX" you are using is of type Brush and not Color
    Tomer Shamam | .NET Technologies Expert | Smart Client Leader | Sela Group
    Thursday, September 25, 2008 8:09 AM
  • From MSDN:

    WindowBrush Gets a SolidColorBrush that is the background color in the client area of a window.

    Friday, September 26, 2008 10:44 AM
  • Old thread I know but if I declare the brush as a static as Tomer suggested:

    <Window.Resources>

            <x:Static x:Key="MyBrush"
                      Member="local:Test.MyBrush" />

    </Window.Resources>

    Then I am unable to reference it as {StaticResource MyBrush} and have to write it as:

       <Grid Background="{StaticResource {StaticResource MyBrush}}" />

    Any ideas how I can redefine a static brush with a resource key and use it in simple fashion?

       <Grid Background="{StaticResource MyBrush}" />

    Tuesday, September 11, 2012 5:12 PM