Override TextBlock style of ContentPresenter in Metro theme - c#

I use metro theme in my project.
I have a style for TextBlock Base.
<Style x:Key="TextBlockText" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF63798F"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
I want to change the foreground of GridViewColumnHeader by setting a style for it. I added TextBlock.Foreground="White" for ContentPresenter of it. But it doesn't change color!
When I remove the TextBlock style it is ok.

Remove Foreground property of TextBlockText
add a style for Windows
Add TextBlock.Foreground='White' for ContentPresenter .

Related

Change Font Size of all UI Elements in using Static Resource from App.xaml in WPF

I'm need to change the font size of all text across the application.
I have tried doing as follows, but that doesn't work:-
<Style x:Key="fontsize" TargetType="{x:Type FrameworkElement}">
<Setter Property="Control.FontSize" Value="20"/>
</Style>
<Style TargetType="{x:Type FrameworkElement}" BasedOn="{StaticResource fontsize}"/>
When I try setting as follows then that works fine but doesn't get applied to all elements & needs to apply that for all different types of elements aperately.
<Style TargetType="TextBlock" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DataGridCell" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="MenuItem" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DatePicker" BasedOn="{StaticResource fontsize}"/>
Also I would like to ask that, is there a way that I can override the Global Style for a particular element, like Heading text should be of different size on a user control?
in App.xaml
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
Create a global style for the window in App.xaml.
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</Application.Resources>
and set that style for the required windows.
<Window x:Class="YourNamespace.MainWindow" Style="{StaticResource WindowStyle}".....>
for overriding the style for a usercontrol
<local:UserControl1>
<local:UserControl1.Style>
<Style TargetType="UserControl">
<Setter Property="FontSize" Value="10"/>
</Style>
</local:UserControl1.Style>
</local:UserControl1>
There are two controls this involves.
You're maybe thinking "hey what about this cell or that calendar".
Their templates show text in a textblock.
When you set Header on a menuitem or content on a label, you get a textblock generated.
You therefore "only" need to set style on both textblock and textbox:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Having said that.
As Clemens pointed out.
The Font size and styling dependency properties are marked as inherits, so if you just have mainwindow then you could just set on that.
It's not just "obvious" that a label ends up with a textblock in it when you set content though. Similarly a menuitem and header. Hence I thought it worth posting this answer.

Need to specify TextBox style on each view in WPF

I'm developing a WPF application using Catel & Orchestra Framework as I did in past.
In this particular application, it seems that if I don't specify the style inside the UserControl's resouces it doesn't apply
So I've to do in each view
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Grid.Resources>
And here's my Application.Xaml's resources
<Application.Resources>
<telerik:EnumToBooleanConverter x:Key="EnumToBooleanConverter"></telerik:EnumToBooleanConverter>
<telerik:InvertedBooleanConverter x:Key="InvertedBooleanConverter"></telerik:InvertedBooleanConverter>
<telerik:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"></telerik:BooleanToVisibilityConverter>
<system:Double x:Key="Width">250</system:Double>
<GridLength x:Key="DefaultRowWidth">250</GridLength>
<GridLength x:Key="DefaultRowHeigth">40</GridLength>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style TargetType="CheckBox">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style TargetType="TextBox">
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style TargetType="{x:Type telerik:RadComboBox}">
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style TargetType="{x:Type telerik:RadDatePicker}">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
</Application.Resources>
The enums, static values and so on are correctly recognized and used, in the case of the TextBlock/CheckBoxes and so on, no. I'm also using the FluentRibbon and Telerik as UI component (as I did in past).
Any suggestion?
Here's the layout without in user control's resource
and with it
You are probably using the StyleHelper which creates all the styles for you (based on naming convention, Default[ControlName]Style (e.g. DefaultTextBlockStyle). Orchestra does provide all of these styles out of the box, and you need to override them on the right layer to make sure they win.
You have a few options:
Specify your own version of DefaultTextBlockStyle and it should work.
Disable the StyleHelper / style forwarders
Override the style on a lower level (e.g. the user control level)

WPF - Change Scrollbar's margin of a TextBox using Styles

I have a defined an implicit style for Scrollbar and set some properties and I use it for most ScrollViewrs. Part of the Style is :
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="Background" Value="#FF283542" />
<Setter Property="Margin" Value="0,-32,0,0" />
<Setter Property="Width" Value="5" />
</Style>
But I have some TextBoxes which I want them to have the same implicit Scrollbar style for their Scrollbars but with a diffrent margin.
I can do it by adding Resources to every TextBox and override the implicit ScrollBar style like :
<TextBox Style="{StaticResource big-text-style}">
<TextBox.Resources>
<Style TargetType="{x:Type ScrollBar}"
BasedOn="{StaticResource {x:Type ScrollBar}}">
<Setter Property="Margin" Value="0"/>
</Style>
</TextBox.Resources>
</TextBox>
This code gives me the functionality I want. But the problem with this approach is that I have to write these lines of code of every TextBox! It would be much better if I could put this as part of the TextBox style itself.
I wonder is there a way to put it in the TextBox's Style so that every TextBox which has big-text-style(for example), have the overriden ScrollBar ?
Or is there a better way to implement this kind of thing ?
Thank you for the help!
You can add the overriding ScrollBar style to the Resources of the TextBox style itself:
<Style x:Key="big-text-style" TargetType="TextBox">
<Style.Resources>
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
<Setter Property="Margin" Value="0"/>
</Style>
</Style.Resources>
</Style>

WPF DatePicker Popup Calendar Background Color

How can I style the background color of the popup / dropdown control of the default WPF DatePicker control?
I want to do this via xaml.
This does not work:
<Style TargetType="{x:Type DatePicker}">
<Setter Property="FontFamily" Value="/fonts/#Titillium Web" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Background" Value="#777777" />
</Style>
I assume that it does not target the right property.
This is how it currently looks. I want to change the white background to something readable.
Try targeting DatePickerTextBox, like so:
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Background" Value="#777777" />
</Style>

Setting foreground for whole Windows Phone's application

How to change foreground color for all controls in the application? I need to change color for: textboxes, textblocks, borders of buttons.
It will take too long to do it one by one (over 100 controls).
This is what styles are for. You can add styles in your app.xaml file. Something like:
<Application.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="White" />
</Style>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="White" />
</Style>
</Application.Resources>
Assuming you are programming for Windows Phone 7.1 (Mango) or later, you can use a Style in your App.xaml file, add the following code inside your Application.Resources tag and customize as needed. The styles will be applied to all Pages in your application (you can still override individual properties directly in the corresponding element tags).
<Application.Resources>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Blue" />
</Style>
</Application.Resources>

Categories

Resources