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

[Solved] How to change style for all cells

9 Answers 301 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Geoff Hardy
Top achievements
Rank 1
Geoff Hardy asked on 10 Jun 2010, 06:28 PM
We recently upgraded to Silverlight 4 and the 2010 Q1 SP2 RadControls for Silverlight. In our app.xaml we are applying a style to all textboxes in our application as follows:

<Style TargetType="TextBox"
    <Setter Property="Margin" Value="4" /> 
</Style> 
 

By specifying a margin for all the TextBoxes, it spaces our TextBoxes out nicely on our forms. However, when using the RadGridView, all of text displayed in the cells now use this margin of 4, which makes the text in each cell more cramped.

Is there an easy way to tell the RadGridView to use a margin of 0 for all TextBoxes that it uses in each cell? Does this need to be specified on each column in the grid?

9 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 11 Jun 2010, 09:12 AM
Hi Geoff Hardy,

 If you want to override the style for textboxes in the GridView only, you need to re-define the style in the Resources tag of the GridView:

<telerik:RadGridView ...>
  <telerik:RadGridView.Resources>
    <Style TargetType="TextBox">
      <Setter Property="Margin" Value"0"/>
    </Style>
  <telerik:RadGridView.Resources>
  ...
</telerik:RadGridView>

Sincerely yours,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Geoff Hardy
Top achievements
Rank 1
answered on 11 Jun 2010, 04:31 PM
Thanks Yavor. This works well.

The only thing I need to do now is find an easy way to apply this to the many RadGridViews in my application. I would like to avoid pasting this code into each one. I tried adding the setting of the resources to a style that is assigned to all of my RadGridViews:

        <Style TargetType="FrameworkElement" x:Key="noMarginStyle">  
            <Setter Property="Margin" Value="0"/>  
        </Style> 
 
        <Style TargetType="Controls2:RadGridView" > 
            <Setter Property="FrameworkElement.Resources">  
                <Setter.Value> 
                    <ResourceDictionary> 
                        <Style TargetType="TextBox" BasedOn="{StaticResource noMarginStyle}"/>  
                        <Style TargetType="TextBlock" BasedOn="{StaticResource noMarginStyle}"/>  
                    </ResourceDictionary> 
                </Setter.Value> 
            </Setter> 
        </Style> 
 

However, assigning the Resources via a style does not work. Declaring the resource dictionary in app.xaml and assigning it to the resources property directly does not work either:

        <ResourceDictionary x:Key="noMargins">  
            <Style TargetType="TextBox" BasedOn="{StaticResource noMarginStyle}"/>  
            <Style TargetType="TextBlock" BasedOn="{StaticResource noMarginStyle}"/>  
        </ResourceDictionary> 
 
    <TelerikGrid:RadGridView Resources="{StaticResource noMargins}">  
...  
    </TelerikGrid:RadGridView> 
 

The only other way I can see to do this would be to create my own custom control that inherits from RadGridView and sets the resource dictionary itself.

Any other suggestions as to how I might accomplish my goal here?
0
Yavor Georgiev
Telerik team
answered on 14 Jun 2010, 09:02 AM
Hello Geoff Hardy,

 Unfortunately, since you're working with Silverlight, I'm afraid I don't see another option short of extending RadGridView and overriding the OnLoaded protected method. If you were working with WPF, you could add a global event handler for the RadGridView's Loaded event.

Greetings,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Geoff Hardy
Top achievements
Rank 1
answered on 25 Nov 2010, 12:48 AM
I recently upgraded to version 2010.3.1119.1040 of the RadControls and this strategy no longer works. Although I copy the styles with no margin into the resource dictionary for our CustomGridView (which inherits from RadGridView), the styles do not get applied.

Is there a newer and better way of achieving this goal in this latest release?

Thanks!

Geoff
0
Yavor Georgiev
Telerik team
answered on 25 Nov 2010, 12:47 PM
Hi Geoff Hardy,

 I have tried to reproduce this behavior, but so far I have been unsuccessful. What I did was to define an implicit style for TextBox in my App.xaml. This style applied to textboxes in the pages, but not to textboxes in RadGridView's columns. Are you using custom cell templates? Could you please share a bit more about your scenario?

Kind regards,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Geoff Hardy
Top achievements
Rank 1
answered on 25 Nov 2010, 05:17 PM
We have a CustomGridView, which extends RadGridVeiw. In its constructor, we set up event handlers to set its styles:

public CustomGridView()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }
            // Inside the grid cells, we do not want large margins, so cells get smaller margins
            Loaded += (sender, e) => this.CopyStyles((ResourceDictionary) Application.Current.Resources["gridViewStyles"]);
  
            // For anything in the row details, use the standard margins used elsewhere in the application
            LoadingRowDetails += (sender, e) => e.DetailsElement.CopyStyles(Application.Current.Resources);
        }

The CopyStyles method looks like this:

public static void CopyStyles(this FrameworkElement frameworkElement, ResourceDictionary resources)
{
    var existingResources = frameworkElement.Resources;
    foreach (var key in resources.Keys)
    {
        var resource = resources[key];
        if (!existingResources.Contains(key) && resource is Style)
        {
            existingResources.Add(key, resource);
        }
    }
}

The styles that we are copying are as follows:

<!-- When controls are displayed inside a grid, we do not want to add a margin around them-->
<ResourceDictionary x:Key="gridViewStyles">
    <Style TargetType="TextBlock" BasedOn="{StaticResource textBlockStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="TextBox" BasedOn="{StaticResource textBoxStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="ComboBox" BasedOn="{StaticResource comboBoxStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="CheckBox" BasedOn="{StaticResource checkBoxStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="Controls1:RadComboBox" BasedOn="{StaticResource radComboBoxStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="Controls1:RadDatePicker" BasedOn="{StaticResource controlStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="Controls1:RadTimePicker" BasedOn="{StaticResource radTimePickerStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
    <Style TargetType="Button" BasedOn="{StaticResource buttonStyle}">
        <Setter Property="Margin" Value="{StaticResource marginInsideGrid}" />
    </Style>
</ResourceDictionary>

This seems to work in most cases, but in one case we have a CustomGridView inside a RadTabControl inside a RadBusyIndicator inside a RadWindow, and the copying of styles does not work. If we define the style explicitly on this control it does work, eg:

<Control:CustomGridView >
    <Control:CustomGridView.Resources>
              <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="0"/>
              </Style>
            </Control:CustomGridView.Resources>

However, as mentioned previously, having to copy and paste these "no margin" styles in everywhere that we  use a CustomGridView is not an ideal solution, since we are using a lot of RadGridViews and this would mean a lot of duplication.

Ideally it would be nice to be able to be able to define this concept that "all controls within a RadGridView have a margin of zero" in one place.
0
Yavor Georgiev
Telerik team
answered on 26 Nov 2010, 10:32 AM
Hi Geoff Hardy,

 I'm trying to replicate your scenario, but so far I have been unsuccessful. Please find attached a sample solution and try and introduce the behavior there.

Greetings,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Riia
Top achievements
Rank 1
answered on 09 Dec 2010, 01:40 PM
Hi,

Is this the normal behavior, how it should be? That if implicit style is defined for all TextBoxes the style does not apply to TextBoxes inside RadGridView? We would like to use same styles but it seems that the default style is used (not the implicit one we defined)? However it seems that DatePicker apply our styles, defined in App.xml.

Could you please tell which is the recommended way to define style for TextBoxes (or other controls) inside RadGridView?

Best Regards,
Riia
0
Yavor Georgiev
Telerik team
answered on 09 Dec 2010, 04:14 PM
Hi Riia,

 Controls inside RadGridView receive their styles from the RadGridView's theme. Framework controls such as TextBox and ScrollBar are styled in our themes. You should edit the source code of one of our existing themes and tweak the System.Windows.Controls.TextBox.xaml file to suit your needs.

Greetings,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Geoff Hardy
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Geoff Hardy
Top achievements
Rank 1
Geoff Hardy
Top achievements
Rank 1
Riia
Top achievements
Rank 1
Share this question
or