locked
Setting property through style for base class does not update property value for derived class RRS feed

  • Question

  • Hello,

    Can someone help me?  I have BaseClass(Derived from UserControl) which declares dependancy property BackGroundColor,

    All my inherited classes use this property to set the background for each of them.

    Now when I set my property using style in my theme file, using base class, this style is not applied for any of inherited class,

    But if I set style using derived class, Style is applied correctly. I have more than 10 derived classes, that may use this property.

    Can Someone tell me, how can I set the property in my theme file for base class and all the derived classes would get it?

    something like,

    <Style TargetType="BaseClass">

    <Setter Property="BaseClass.BackGroundBrush"

    Value="{StaticResource RPSBackGroundBrush}"/>

    </Style>

     

    Wednesday, July 1, 2009 9:47 PM

Answers

  • Hi,

    Silverlight 3 has new feature called "BasedOn", it can help you to remove style redundancy.

    sample xaml 

        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
                <Style TargetType="FrameworkElement" x:Name="fe_style">
                    <Setter Property="Width" Value="100"/>
                    <Setter Property="Height" Value="50"/>
                </Style>
                <Style TargetType="Button" BasedOn="{StaticResource fe_style}" x:Name="bn_style">
                    <Setter Property="Content" Value="Hello,world"/>
                </Style>
            </Grid.Resources>
            <Button Style="{StaticResource bn_style}"/>
        </Grid>

    here is a link about "BasedOn"

    http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/06/22/silverlight-3-quick-tip-10-styling-improvements.aspx

    Hope this helps

    Tuesday, July 7, 2009 12:02 AM