Style lost when using StyleSelector

3 posts, 0 answers
  1. Edward
    Edward avatar
    84 posts
    Member since:
    Feb 2011

    Posted 19 Oct 2011 Link to this post

    What I did was I copied the style file from SummerGridViewRow.xaml, then I modified it (basically just remove the gradient background and make the border on selected item thicker) and put it in my project.  That worked as expected on gridview that does not use the any StyleSelector.

    The problem is when I use a StyleSelector, it seems that my customization is gone and it will just use the default style for that theme (in my case, summer with background ).  I supposed the line var style = new Style(typeofGridViewRow) was getting the default style from the assembly instead of my customization.  My selector look like this:

    public override Style SelectStyle(object item, DependencyObject container)
            {
                var style = new Style(typeof(GridViewRow));
     
                var row = (GridViewRow)container;
     
                    var obj = item as something;
     
                    if (somecondition)
                        style.Setters.Add(new Setter(GridViewRow.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 245, 208, 206))));
                return style;
            }

    My CustomGridView.xaml (remove a lot of line for space saving).  The file is included in App.xaml as merged resource dictionary:

    <SolidColorBrush x:Key="GridView_RowIndicatorCellBackground_Selected" Color="#FFF5F6F8"/>
        <SolidColorBrush x:Key="ItemOuterBorder_Over" Color="#FFC2DAE3" />
        <LinearGradientBrush x:Key="ItemInnerBorder_Over" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFE7F3F8" Offset="0"/>
            <GradientStop Color="#FFD4E7EE" Offset="1"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ItemBackground_Over" Color="#FFE7F3F8" />
            <!--<GradientStop Color="#FFE7F3F8" Offset="0"/>
            <GradientStop Color="#FFD4E7EE" Offset="1"/>-->
             
    --- many other lines regarding colors ---
     
        <telerik:BooleanToOpacityConverter x:Key="BooleanToOpacityConverter" />
        <telerik:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <grid:GridLineWidthToThicknessConverter x:Key="GridLineWidthToThicknessConverter" />
     
        <ControlTemplate x:Key="GridViewRowTemplate" TargetType="grid:GridViewRow">
    ---- control template ----
        </ControlTemplate>



    How do I make it so that, when using a style selector it will return my customized style?
  2. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Oct 2011 Link to this post

    Hello Edward,



    The styles used in our themes are theme specific. When you override the background property in the StyleSelector, it does not recognize that the theme is different and fallbacks to the default one - Office_Black. As a side note If you set respective Style or DataTemplate property (for example CellStyle/CellStyleSelector) the Style/DataTemplate selector will not be applied. What you may do is to keep 2 styles (Summer theme specific with their corresponding templates) and return the appropriate one based on your needs:


    <Style x:Key="HighUnitPriceStyle" TargetType="telerik:GridViewRow">
                    <Setter Property="Template" Value="{StaticResource HighUnitPriceTemplate}" />
                    ....
                </Style>
                <Style x:Key="LowUnitPriceStyle" TargetType="telerik:GridViewRow">
                    <Setter Property="Template" Value="{StaticResource LowUnitPriceTemplate}" />
    ....
                </Style>
     
     
    <Examples:UnitPriceConveter x:Key="converter" />
                <Examples:ConditionalStyleSelector x:Key="selector" ConditionConverter="{StaticResource converter}">
                    <Examples:ConditionalStyleSelector.Rules>
                        <Examples:ConditionalStyleRule Style="{StaticResource HighUnitPriceStyle}">
                            <Examples:ConditionalStyleRule.Value>
                                <sys:Boolean>True</sys:Boolean>
                            </Examples:ConditionalStyleRule.Value>
                        </Examples:ConditionalStyleRule>
                        <Examples:ConditionalStyleRule Style="{StaticResource LowUnitPriceStyle}">
                            <Examples:ConditionalStyleRule.Value>
                                <sys:Boolean>False</sys:Boolean>
                            </Examples:ConditionalStyleRule.Value>
                        </Examples:ConditionalStyleRule>
                    </Examples:ConditionalStyleSelector.Rules>
                </Examples:ConditionalStyleSelector>


    You may take a look at our online demos for further reference.



    Greetings,
    Vanya Pavlova
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Edward
    Edward avatar
    84 posts
    Member since:
    Feb 2011

    Posted 19 Oct 2011 Link to this post

    Thanks, in my case I just add:

    if (somecondition)
        style.Setters.Add(new Setter(GridViewRow.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 245, 208, 206))));
    else
        style.Setters.Add(new Setter(GridViewRow.TemplateProperty, App.Current.Resources["GridViewRowTemplate"] as ControlTemplate));

    That will take the new control template.
Back to Top