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

10 posts, 1 answers
  1. danparker276
    danparker276 avatar
    389 posts
    Member since:
    Aug 2010

    Posted 23 Nov 2011 Link to this post

    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?
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 28 Nov 2011 Link to this post

    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 >>

  3. DevCraft banner
  4. danparker276
    danparker276 avatar
    389 posts
    Member since:
    Aug 2010

    Posted 29 Nov 2011 Link to this post

    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
  5. Answer
    Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 29 Nov 2011 Link to this post

    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 >>

  6. Andir
    Andir avatar
    2 posts
    Member since:
    May 2014

    Posted 14 May 2014 in reply to Vanya Pavlova Link to this post

    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.
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 15 May 2014 Link to this post

    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.
     
  8. Andir
    Andir avatar
    2 posts
    Member since:
    May 2014

    Posted 22 May 2014 in reply to Dimitrina Link to this post

    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.
  9. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 27 May 2014 Link to this post

    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.
     
  10. Christophe
    Christophe avatar
    1 posts
    Member since:
    Feb 2013

    Posted 23 Jan 2015 in reply to Dimitrina Link to this post

    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
  11. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 24 Jan 2015 Link to this post

    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.

     
Back to Top
DevCraft banner