Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Is there a way to set all the controls font size and type same
Posted
Updated 22-Jun-11 23:09pm
v2

Possibly the easiest way to do this is to add a resource file and set the size, and font in there. Here's an example ResourceDictionary that you can use:
XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="32" />
        <Setter Property="FontFamily" Value="Verdana" />
    </Style>
    <Style TargetType="{x:Type Control}" x:Key="fontStyling">
        <Setter Property="FontSize" Value="32" />
        <Setter Property="FontFamily" Value="Verdana" />
    </Style>
    <Style TargetType="{x:Type Label}" BasedOn="{DynamicResource fontStyling}" />
    <Style TargetType="{x:Type TextBox}" BasedOn="{DynamicResource fontStyling}" />
    <Style TargetType="{x:Type ListView}" BasedOn="{DynamicResource fontStyling}" />

    <!-- Add other targets such as CheckBox here. -->

</ResourceDictionary>
You just need to add this is a reference to your app.xaml and you are good to go - it will be picked up throughout your project.

You may be wondering why I declared TextBlock separately from Control. This is because TextBlock does not derive from Control, so you can't use Control as a base style for the TextBlock.
 
Share this answer
 
Comments
arun_pk 23-Jun-11 5:43am    
Thanks Pete


I am not having an app.xaml
coz here in my project i am using my wpf contol in winform..
so how can i use these styles in my user controls....
soorajmamar 19-Sep-13 8:02am    
Hi im using blend 1 there i created for ResourceDictionary separate project try to use by dll reference from another but getting error "file path not found"


"resourcedictionaryPractice1" assembly name

<usercontrol.resources>
<resourcedictionary>
<resourcedictionary.mergeddictionaries>
<resourcedictionary source="pack://application:,,,/resourcedictionaryPractice1;component/Dictionary1.xaml">


Pete O'Hanlon 23-Jun-11 6:07am    
In your user controls (all of them I'm afraid), you can add a reference to your resource dictionary. You'd normally do this like <usercontrol.resources><resourcedictionary source="MyDictionary.xaml"> (assuming that MyDictionary.xaml was in the same folder as the user control).
arun_pk 23-Jun-11 6:09am    
Yes I already have a dictionary
I have added the snippets which you provided above

But i wonder how the styles will be used with out any key...
sorry i am new to wpf :(
Pete O'Hanlon 23-Jun-11 6:14am    
Basically, if you add a key, then you need to explicitly set the style against the relevant control. Without the key, it gets applied to all controls of that type - so, setting TargetType="{x:Type TextBlock}" without a key tells the system 2 things:

1. That the control type that the properties belong to is a TextBlock - without this info, it wouldn't know whether or not a property could be set.
2. Apply the styling to all instances of that type.
apply themes or style on xaml files.

check this link[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900