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

Can't change cell style if default textblock is set in styles

9 Answers 359 Views
GridView
This is a migrated thread and some comments may be shown as answers.
danparker276
Top achievements
Rank 2
danparker276 asked on 23 Nov 2011, 11:56 PM
If I'm using CellStyleSelector or just changing the cell style or foreground in cell edit ended, it won't let me override a value set in the default style for textblock.  For example I have in styles:
<Style TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource BodyTextColorBrush}"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="12"/>
</Style>
There's no way for me to change the foreground value now.

Something similar has happened before like this to me.  Is setting a default textblock in the styles common?  Otherwise I would think a lot more people would have seen this error.  Is there a better way for me to set the defaults?

9 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 28 Nov 2011, 09:32 AM
Hi Danparker276,

 To set the default cell style, you would better target the GridViewCell rather than the TextBlock. For example:

<Style TargetType="telerik:GridViewCell">
    <Setter Property="Foreground" Value="{StaticResource BodyTextColorBrush}"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="12"/>
</Style>

This will resolve your issue.
Greetings,
Didie
the Telerik team

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

0
danparker276
Top achievements
Rank 2
answered on 29 Nov 2011, 09:46 PM
I don't care about the default value, I want to change it.  But that doesn't really matter.  Setting the default GridViewCell doesn't work either.
<Style TargetType="TextBlock">
    <Setter Property="Foreground" Value="Blue"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="telerik:GridViewCell">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="FontSize" Value="12"/>
</Style>
When I make a grid with this, I get Blue text in my cells unless I have a combobox column
0
Accepted
Vanya Pavlova
Telerik team
answered on 29 Nov 2011, 10:17 PM
Hello Danparker276,

 

The behavior you get is the expected one! An implicit style targeted at a TextBlock element will affect all text elements in RadGridView. If you have defined an implicit style targeted at a TextBlock and you do not need it only in RadGridView through out your application you should ignore this style at a RadGridView level. This can be achieved through defining an implicit style targeted at a TextBlock element within RadGridView's Resource Collection:


<UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
    </UserControl.Resources>
  
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <telerik:RadGridView x:Name="grid"  ItemsSource="{Binding Collection}" ........>
            <telerik:RadGridView.Resources>
                <Style TargetType="TextBlock"/>
                </telerik:RadGridView.Resources>
    </telerik:RadGridView>
    </Grid>


Regards,
Vanya Pavlova
the Telerik team

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

0
Andir
Top achievements
Rank 1
answered on 14 May 2014, 02:54 PM
Hi,

Please, forgive me for up the old topic.

I believe that this is a bug and/or counter-intuitive behavior: implicit styles should be overriden by control styles.
Expected behavior you can observe for standard controls (includes telerik controls): only GridViewCell Style doesn't override implicit Textblock style:

<Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="Blue" />
            </Style>
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="Foreground" Value="Red" />
            </Style>
            <Style x:Key="RadListBoxItemStyle" TargetType="telerik:RadListBoxItem">
                <Setter Property="Foreground" Value="Red" />
            </Style>
            <Style x:Key="RadGridViewCellStyle" TargetType="telerik:GridViewCell">
                <Setter Property="Foreground" Value="Red" />
            </Style>
        </Grid.Resources>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button Content="Button" Style="{StaticResource ButtonStyle}" />
            <telerik:RadButton Content="RadButton" Style="{StaticResource ButtonStyle}" />
            <telerik:RadListBox ItemContainerStyle="{StaticResource RadListBoxItemStyle}" ItemsSource="{Binding Items}" />
            <telerik:RadGridView AutoGenerateColumns="False" ItemsSource="{Binding Items}">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn CellStyle="{StaticResource RadGridViewCellStyle}" DataMemberBinding="{Binding}" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </StackPanel>
    </Grid>

See an attached screenshot.

Please, investigate and fix that behavior.
0
Dimitrina
Telerik team
answered on 15 May 2014, 03:04 PM
Hi,

The Foreground of a TextBlock will be applied to all the TextBlock elements. In order to not have it overriding the Style defined for GridViewCell, you need to override this base Style in RadGridView.Resources.

For example:
<telerik:RadGridView.Resources>
  <Style TargetType="TextBlock"/>
</telerik:RadGridView.Resources>
 
That way the Foreground set for GridViewCell should be respected.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Andir
Top achievements
Rank 1
answered on 22 May 2014, 01:26 PM
Thank you, Didie, but I'm already know this workaround. This is not a fix.

I believe that this is a bug (see my examples in prev. message), all standard microsoft controls and other telerik controls do not follow such behavior and override implicit styles if control style is set (see my screenshot in prev.message), only gridview works in that unexpected way and demand to reset implicit style explicitly.

Please investigate and fix.
0
Yoan
Telerik team
answered on 27 May 2014, 11:18 AM
Hi,

I am afraid that we can't provide a fix for this. By default RadGridView has different properties (like the Foreground of GridViewCells) which are implicitly set. We cannot change this since it would result in many breaking changes. Still, you can use the workaround suggested by my colleague Didie.

Regards,
Yoan
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Christophe
Top achievements
Rank 1
answered on 23 Jan 2015, 05:02 PM
Hi,

I am having the same issue, but the RadGridView I am using is created in the code behind. Could you advise how to set the resources in the code behind.

Thanks
0
Ivan Ivanov
Telerik team
answered on 24 Jan 2015, 02:34 PM
Hello,

Although this is neither a common, nor a recommended approach, you can create an implicit style in code behind like this:
ResourceDictionary res = new ResourceDictionary();
             
            var baseStyle = Application.Current.Resources["GridViewCellStyle"] as Style;
            Style st2 = new Style();
            st2.TargetType = typeof(GridViewCell);
            st2.BasedOn = baseStyle;
            st2.Setters.Add(new Setter() { Property = GridViewCell.ForegroundProperty, Value = new SolidColorBrush(Colors.Blue) });
            st2.Seal();
 
            res.Add(typeof(GridViewCell), st2);
            this.Resources.MergedDictionaries.Add(res);
 
            InitializeComponent();
            RadGridView rgv = new RadGridView();
            MyViewModel mvm = new MyViewModel();
            rgv.ItemsSource = mvm.Clubs;
 
            grid.Children.Add(rgv);

Regards,
Ivan Ivanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
danparker276
Top achievements
Rank 2
Answers by
Dimitrina
Telerik team
danparker276
Top achievements
Rank 2
Vanya Pavlova
Telerik team
Andir
Top achievements
Rank 1
Yoan
Telerik team
Christophe
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or