I'm trying to add a global style (Font size and Font family) into my WPF application for Window I have, but no style Is applied to It, whatever I do. I think my problem Is that my startup Window Is not App.xaml, because I use App.xaml just to check If user has permission to run application. But right after that my desired Window opens, so StartupUri in my App.xaml Is set to that Window.
Here is my App.xaml:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
StartupUri="FirstWindowToShow.xaml">
<Application.Resources>
<!--Style that should be applied to all Windows-->
<Style x:Key="Win_style" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
<!--Style for all Pages - works fine-->
<Style x:Key="PageFont" TargetType="{x:Type Page}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="12" />
</Style>
</Application.Resources>
</Application>
And here is my FirstWindowToShow.xaml :
<Window x:Class="MyApp.FirstWindowToShow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Priprava_Podatkov"
mc:Ignorable="d"
Title="Some title" Height="480" Width="800" Loaded="Window_Loaded" Background="#FFF9F9F9" OpacityMask="Black">
<Grid>
<Menu x:Name="G_Menu" HorizontalAlignment="Left" VerticalAlignment="Top" Height="20" Width="792">
<MenuItem x:Name="Menu_Program">
<MenuItem x:Name="Menu_V" Header="About" Click="Menu_V_Click"/>
<MenuItem x:Name="Menu_End" Header="Close" Click="Menu_End_Click"/>
</MenuItem>
<MenuItem Header="Department 1" Height="20" Width="148">
<MenuItem x:Name="Dept_1" Header="Custom controlling" Click="Dept_1_Click"/>
</MenuItem>
</Menu>
<Frame x:Name="Frame_s" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="772" NavigationUIVisibility="Hidden"/>
<StatusBar DockPanel.Dock="Bottom" Margin="0,386,0,0" VerticalAlignment="Bottom" Background="Transparent">
<StatusBarItem Width="73">
<Label Content="User:" FontWeight="Bold" Width="73"/>
</StatusBarItem>
<StatusBarItem>
<Label x:Name="LblU" Content="user" FontWeight="Light"/>
</StatusBarItem>
<StatusBarItem>
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Height="10" />
</StatusBarItem>
<StatusBarItem>
<Label Content="User permissions:" FontWeight="Bold" />
</StatusBarItem>
<StatusBarItem>
<Label x:Name="LblN" Content="Rights" FontWeight="Light"/>
</StatusBarItem>
<StatusBarItem >
<Label x:Name="Lbl_P" Content="Data exported..." >
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Resources>
<Storyboard x:Key="flashAnimacija">
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:1.5" RepeatBehavior="Forever" />
</Storyboard>
</Style.Resources>
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName= Progress_DoKonca, Path= IsVisible}" Value="True">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger.EnterActions>
<BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimacija}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="flash"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Right" Margin="-10,0,10,0">
<Grid>
<ProgressBar x:Name="Progress_TillEnd" Width="150" Height="20" />
<TextBlock x:Name="Progress_Txt" Text="{Binding ElementName=Progress_DoKonca, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</StatusBarItem>
</StatusBar>
</Grid>
</Window>
I have been trying all sorts of things in code or XAML, like this or this, but still with no success. What am I doing wrong ?
This is what I've done in the past, so see if it works for you:
In your App.xaml, remove the x:Key from the Window style, so it becomes:
<!--Style that should be applied to all Windows-->
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
Then in your App.xaml.cs (code-behind), override the OnStartup method and add this code:-
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
{
DefaultValue = FindResource(typeof(Window))
});
}
This will apply those styles in the App.xaml Window style (i.e. FontFamily and FontStyle) to all windows created by the application.
For the controls like Menu and StatusBar it is necessary to set the style explicitly like below:
<Style x:Key="BaseStyle" TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="13" />
</Style>
<Style TargetType="{x:Type StatusBar}" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="{x:Type Menu}" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="{x:Type local:Window1}" BasedOn="{StaticResource BaseStyle}" />
Why would a Style with an x:Key of "Win_style" be applied to all windows?
You could keep Win_style and define an implict Style per window type (e.g. FirstWindowToShow) that is based on Win_style:
<Application.Resources>
<!--Style that should be applied to all Windows-->
<Style x:Key="Win_style" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
<!-- implicit window styles, one for each window -->
<Style TargetType="{x:Type local:FirstWindowToShow}" BasedOn="{StaticResource Win_style}" />
<!--Style for all Pages - works fine-->
<Style x:Key="PageFont" TargetType="{x:Type Page}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="12" />
</Style>
</Application.Resources>
Related
How I can define style in App.xaml for CustomButton?
App.xaml
<Style x:Key="CustomButtonSmall" TargetType="CustomButton">
<Setter Property="FontSize" Value="14" />
</Style>
MyPage.xaml
<local:CustomButton Text="{i18n:Translate CreateAccountButton}"
Grid.Column="0" Command="{Binding CreateAccountCommand}"
Type="Normal" Style="{StaticResource CustomButtonSmall}" />
You define the style in for example Window.xaml:
<Window>
<Window.Resources>
<Style x:Key="myStyle" TargetType="Button">
<Setter Property="Background" Value="Orange" />
<Setter Property="FontStyle" Value="Italic" />
</Style>
</Window.Resources>
Then u target ur button with this:
<Button Style="{StaticResource myStyle}">Buttontext</Button>
In App.xaml
Give CustomButton whole path like TargetType="Control:CustomButton"
and define Control at the top like
xmlns:Control="clr-namespace:xyz"
<Style x:Key="CustomButtonSmall" TargetType="Control:CustomButton">
<Setter Property="FontSize" Value="14" />
</Style>
I have to work with a 1800-Row-XAML-Definition for a single window and I want to reduce the amount of code drastically.
There are several control-definitions, which are repeated very often and I want to write Styles for some of them. One example is a Border-Definition with integrated TextBox:
<Border Grid.Column="2" Margin="1,1,5,0" Background="#bbc2ce">
<my:RibbonTextBox HorizontalContentAlignment="Center"
IsReadOnly="True" Background="#FAFAFA"
Text="{Binding Path=someViewModel.Item,Mode=OneWay}"
MinHeight="0" FontSize="12" FontWeight="Bold" FontFamily="Arial"/>
</Border>
Apart of the Binding Path, every value is exact the same over and over again. So I wrote this Style for the RibbonTextBox:
<Style TargetType="my:RibbonTextBox" x:Key="StandardRibbonTextBox">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="Background" Value="#FAFAFA" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
Now I want to write a style for above border, and integrate the RibbonTextBox Style. Here I am so far:
<Style TargetType="Border" x:Key="borderStyle">
<Setter Property="Background" Value="#bbc2ce" />
</Style>
Is there a possibility to integrate my TextBox-Style here? And if not, does somebody know, how to resolve this issue?
Thanks in advance!
If you only need the readonly textbox, you can use DataTemplate with ContentPresenter as coded below (replaced my:RibbonTextBox with simple TextBox for demonstration). If you need read-write, you have to provide some way of binding that allows read-write. This can be achieved with ControlTemplate (also in sample code).
<Grid x:Name="grid1">
<Grid.Resources>
<Style TargetType="TextBox" x:Key="StandardRibbonTextBox">
<!--Changed that one from HorizontalAlignment, in comparison to the question-->
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="Background" Value="#FAFAFA" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
<Style TargetType="Border" x:Key="borderStyle">
<Setter Property="Background" Value="#bbc2ce" />
<Setter Property="Margin" Value="1,1,5,0"/>
<Setter Property="Padding" Value="5"/>
</Style>
<DataTemplate x:Key="borderedTextboxTemplate" DataType="{x:Type sys:String}">
<Border Style="{StaticResource borderStyle}">
<!--This is not going to work for two way binding (IsReadOnly="False" and Text="{Binding Mode=TwoWay}")-->
<TextBox Style="{StaticResource StandardRibbonTextBox}" Text="{Binding Mode=OneWay}"/>
</Border>
</DataTemplate>
<ControlTemplate x:Key="borderedTextboxControlTemplate" TargetType="TextBox">
<Border Style="{StaticResource borderStyle}">
<!--This is not going to work for two way binding (IsReadOnly="False" and Text="{Binding Mode=TwoWay}")-->
<TextBox Style="{StaticResource StandardRibbonTextBox}" Text="{Binding Text,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
</Border>
</ControlTemplate>
<Style x:Key="borderedTextboxControlStyle" TargetType="TextBox">
<Setter Property="Template" Value="{StaticResource borderedTextboxControlTemplate}"/>
</Style>
</Grid.Resources>
<StackPanel Margin="10">
<!--Using Controls directly-->
<Border Style="{StaticResource borderStyle}">
<TextBox
Text="{Binding Path=someViewModel.Item,Mode=OneWay}"
Style="{StaticResource StandardRibbonTextBox}"/>
</Border>
<Separator Margin="10"/>
<!--Using DataTemplate-->
<ContentPresenter
Content="{Binding Path=someViewModel.Item,Mode=OneWay}"
ContentTemplate="{StaticResource borderedTextboxTemplate}"/>
<Separator Margin="10"/>
<!--Using ControlTemplate via Style-->
<TextBox Text="{Binding Path=someViewModel.Item,Mode=OneWay}" Style="{StaticResource borderedTextboxControlStyle}"/>
</StackPanel>
</Grid>
I've created a custom style for the context menu. But I have to put 3 lines of code under each widget(Textbox):
<TextBox.ContextMenu>
<ContextMenu/>
</TextBox.ContextMenu>
Why do I have to do that ? My tooltip style works without any extra code.
My context menu code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BackgroundColor" A="255" R="19" G="19" B="19"/>
<BitmapImage x:Key="BiCut" UriSource="Images/cut.tif"/>
<BitmapImage x:Key="BiCopy" UriSource="Images/copy.tif"/>
<BitmapImage x:Key="BiPaste" UriSource="Images/paste.tif"/>
<SolidColorBrush x:Key="BorderBrush" Color="#ECECEC"/>
<Style TargetType="ContextMenu">
<Setter Property="Foreground" Value="{StaticResource BorderBrush}"/>
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Grid.IsSharedSizeScope" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContextMenu">
<Border BorderThickness="1" BorderBrush="#2468d9" Padding="2" Background="#131313">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Height="20">
<Image Source="{StaticResource BiCut}" Width="20"/>
<Button Content="Ausschneiden" Margin="5,0,0,0"/>
<TextBlock Text="Strg+X" TextAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Controls do not have ContextMenu by default (check property - it is null). So your style will be not applied to any Control.
Tooltip is null by defult, but it will be filled by TooltipService if it is necessary.
There is no service to fill your ContextMenu for all of your element. Use a default style for this (for Control).
You can create a default Style for your TextBox and add that ContextMenu in Style. In this way you will have that ContextMenu for every TextBox you will add in your application and you won't have to add these three lines everywhere.
<Style TargetType="{x:Type TextBox}" >
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu />
</Setter.Value>
</Setter>
</Style>
I want to write a style for wpf where all buttons in a StatusBar (that has a defined style) have the same style (e.g. width).
Here is what my style looks like:
<Style TargetType="{x:Type StatusBar}"
x:Key="DialogBoxStatusBarStyle">
<Setter Property="Background"
Value="LightGray" />
<Setter Property="Padding"
Value="5" />
...?
</Style>
And the xaml for the elements:
<StatusBar Style="{StaticResource ResourceKey=DialogBoxStatusBarStyle}" Grid.Row="3"
FlowDirection="RightToLeft">
<Button Content="Übernehmen"
Width="100"
HorizontalAlignment="Right" />
<Button Content="Abbrechen"
Width="100"
HorizontalAlignment="Right" />
<Button Content="OK"
Width="100"
HorizontalAlignment="Right" />
</StatusBar>
In the final version I don't want to set width to 100 for all buttons. This should be defined in the style of the StatusBar or better say in the style of the button-childs of the StatusBar.
You could add a default Style for Buttons to the Resources of your DialogBoxStatusBarStyle:
<Style TargetType="StatusBar" x:Key="DialogBoxStatusBarStyle">
<Style.Resources>
<Style TargetType="Button">
<Setter Property="Width" Value="100"/>
</Style>
</Style.Resources>
...
</Style>
To extend on the answer above (#Clemens), you could even do something like this, to reuse a button style independently, and also apply it to children of a specific container.
Styles:
<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle">
<Setter Property="Width" Value="100" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style TargetType="{x:Type StatusBar}" x:Key="DialogBoxStatusBarStyle">
<Setter Property="Background" Value="LightGray" />
<Setter Property="Padding" Value="5" />
<Style.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MyButtonStyle}" />
</Style.Resources>
...
</Style>
How can I set different fonts for tabitem header and content of this tabitem??
Like anything in WPF, there are many ways. Without knowing exactly what you are trying to do, here is one "for instance" (I wouldn't suggest using this combination of fonts:) )
<TabControl>
<TabControl.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="20" />
</Style>
<Style x:Key="headerStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Control.FontFamily" Value="Papyrus" />
<Setter Property="Control.FontSize" Value="12" />
</Style>
</TabControl.Resources>
<TabItem>
<TabItem.Header>
<TextBlock Text="Header" Style="{StaticResource headerStyle}" />
</TabItem.Header>
<TextBlock Text="Here is the content" />
</TabItem>
</TabControl>