This is a migrated thread and some comments may be shown as answers.

Style lost when using StyleSelector

2 Answers 46 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edward
Top achievements
Rank 1
Edward asked on 19 Oct 2011, 01:27 PM
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 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 19 Oct 2011, 01:46 PM
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 >>

0
Edward
Top achievements
Rank 1
answered on 19 Oct 2011, 01:52 PM
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.
Tags
GridView
Asked by
Edward
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Edward
Top achievements
Rank 1
Share this question
or