Hi,@Srinivas AK . Welcome Microsoft Q&A.
You could refer to the following code to use ResourceDictionary . You can also refer here for more details.
ResourceDictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBlock}" x:Key="textBlockStyle">
<Setter Property="FontSize" Value="26" />
<Setter Property="FontFamily" Value="Calibri" />
</Style>
<Style TargetType="{x:Type Control}" x:Key="fontStyling">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontFamily" Value="Calibri" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource fontStyling}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource fontStyling}" />
</ResourceDictionary>
App.xaml:
<Application.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>
MainWindow.xaml:
<TextBlock x:Name="tb" Width="100" Height="60" Style="{StaticResource ResourceKey=textBlockStyle}" Text="hello"/>
<TextBlock x:Name="tb1" Width="100" Height="60" Text="hello"/>
<TextBox x:Name="tb2" Width="100" Height="60" Text="hello"/>
<Label x:Name="lb" Width="100" Height="60" Content ="hello"/>
The result:
Update:
TextBlock is not derived from Control, but directly from FrameworkElement. There is no common class between TextBlock and Control that has FontSize and FontFamily. They both implement it separately. What you can do to create styles for FrameworkElement, set attached properties TextElement.FontSize and TextElement.FontFamily
<Style TargetType="{x:Type FrameworkElement}" x:Key="fontStyling">
<Setter Property="TextElement.FontSize" Value="16" />
<Setter Property="TextElement.FontFamily" Value="Calibri" />
<Setter Property="TextElement.Foreground" Value="Red" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource fontStyling}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource fontStyling}" />
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource fontStyling}" />
MainWindow:
<TextBlock x:Name="tb1" Width="100" Height="60" Text="hello"/>
<TextBox x:Name="tb2" Width="100" Height="60" Text="hello"/>
<Label x:Name="lb" Width="100" Height="60" Content ="hello"/>
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.