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