How to apply style on a window in Xaml - wpf

I tried to apply style on a window in Xaml. But my code is not applying the style. Can anyone help me out in this problem?
<Window x:Class="Shweta.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5" Height="300" Width="300">
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Verdana"></Setter>
<Setter Property="Foreground" Value="LightBlue"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*" />
<ColumnDefinition Width="238*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="47*" />
<RowDefinition Height="214*" />
</Grid.RowDefinitions>
<Grid Grid.Column="1">
<Button Margin="0,0,114,16" Content="shweta"/>
</Grid>
</Grid>
</Window>

Instead of putting the Style into the Window's Resources, you could directly assign it to the Style property of the Window:
<Window x:Class="Shweta.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5" Height="300" Width="300">
<Window.Style>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="Foreground" Value="LightBlue"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="Background" Value="LightBlue"/>
</Style>
</Window.Style>
...
</Window>

You have to put your style in the Resources of your App.xaml.
<Application.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily"
Value="Verdana"></Setter>
<Setter Property="Foreground"
Value="LightBlue"></Setter>
<Setter Property="FontWeight"
Value="Normal"></Setter>
<Setter Property="WindowStyle"
Value="None"></Setter>
<Setter Property="Background"
Value="LightBlue"></Setter>
</Style>
</Application.Resources>
When you define your Style in the Window it'll be for the children of the Window but not the Window itself.

Related

How to put ContextMenu into Window.Resources

As you can see I am able to put Height property and Foreground property to the Window.Resources
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="50"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Text="StackOverFlow">
<TextBox.ContextMenu>
<ContextMenu Background="Blue"/>
</TextBox.ContextMenu>
</TextBox>
</Grid>
</Window>
How can I put <ContextMenu Background="Blue"/> to the Window.Resources?
You can just put the context menu into separate resource
<Window.Resources>
<ContextMenu x:Key="ContextMenu" Background="Blue"/>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Height" Value="50"/>
<Setter Property="Foreground" Value="Green"/>
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Text="StackOverFlow"/>
</Grid>

TextBlock rendering settings in WPF

I am using a TextBlock style.But it looks bad.
Am I missing code may have been written?
How can I solve this problem?
Here is code:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF353535"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="TextOptions.TextHintingMode" Value="Fixed"/>
<Setter Property="RenderOptions.ClearTypeHint" Value="Auto"/>
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType"/>
<Setter Property="TextOptions.TextFormattingMode" Value="Ideal"/>
<Setter Property="FontFamily" Value="Tahoma"/>
</Style>
TextFormattingMode.Ideal is far from Ideal :). You should use Display for small-to-medium text. More on text formatting here.
Here they are compared:
<Window
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:WpfApplication9" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF353535" />
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="TextOptions.TextHintingMode" Value="Fixed" />
<Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />
<Setter Property="TextOptions.TextRenderingMode" Value="ClearType" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="FontFamily" Value="Tahoma" />
</Style>
</Window.Resources>
<StackPanel Margin="10">
<TextBlock>This text has TextOptions.TextRendering mode set to Display</TextBlock>
<TextBlock TextOptions.TextFormattingMode="Ideal">This text has TextOptions.TextRendering mode set to Ideal</TextBlock>
</StackPanel>
</Window>

Why does VerticalScrollBarVisibility not work in a style in Silverlight?

VerticalScrollBarVisibility works when I define it inline like this:
<UserControl x:Class="TestScrollBar.MainPage"
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">
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="EditListContainerContentMultiLineTwoColumn">
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="300"/>
<Setter Property="IsReadOnly" Value="False"/>
<Setter Property="Margin" Value="0 0 0 20"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<StackPanel HorizontalAlignment="Left">
<TextBox Text="this is a test"
Style="{StaticResource EditListContainerContentMultiLineTwoColumn}"
VerticalScrollBarVisibility="Auto"
/>
</StackPanel>
</Grid>
</UserControl>
But when I put VerticalScrollBarVisibility in a style, it shows me a blank screen:
<UserControl x:Class="TestScrollBar.MainPage"
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">
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="EditListContainerContentMultiLineTwoColumn">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="300"/>
<Setter Property="IsReadOnly" Value="False"/>
<Setter Property="Margin" Value="0 0 0 20"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<StackPanel HorizontalAlignment="Left">
<TextBox Text="this is a test"
Style="{StaticResource EditListContainerContentMultiLineTwoColumn}"
/>
</StackPanel>
</Grid>
</UserControl>
In WPF it works works fine.
How can I get VerticalScrollBarVisibility to work in a style?
It is not working because these properties are not dependency ones and cannot be applied with style. Unfortunately using the attached property on the ScrollViewer is also not working, because they are not template bound in the default style.
The only thing I can think of you can do is to create an attached behavior that is setting the required values on the text box and apply it through the style.

How to apply style in WPF Controls?

I am beginner on WPF and need your help.
Problem:
I have 4 buttons on the form and need to apply 2 different style on pair of 2 buttons.
Is there any way we can achive this ?
please provide me sample if possible...
Thanks in advance...
You can define named styles and then assign them explicitly to any controls as you wish.
Here is a primer for styling buttons: Getting Started with WPF : Button Control Part 2 – Basic Styling
And here is an example:
<Window x:Class="WpfButtonStyling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="400">
<Window.Resources>
<Style x:Key="ButtonStyle1"
TargetType="{x:Type Button}">
<Setter Property="Foreground"
Value="Red" />
<Setter Property="Margin"
Value="10" />
</Style>
<Style x:Key="ButtonStyle2"
TargetType="{x:Type Button}">
<Setter Property="Foreground"
Value="Blue" />
<Setter Property="Margin"
Value="10" />
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<Button x:Name="FirstButton"
Content="First!"
Style="{StaticResource ButtonStyle1}"/>
<Button x:Name="SecondButton"
Content="Second"
Style="{StaticResource ButtonStyle2}" />
</StackPanel>
</Grid>
</Window>
If someone want to write Style directly in Button, write like below:
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="TimesNewRoman" />
<Setter Property="FontSize" Value="50"/>
<Setter Property="Background" Value="Green"/>
</Style>
</Button.Style>
</Button>
Use this Code For different styles for different buttons or any other
<Window x:Class="WpfApplication1.MainWindow"
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"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Window.Resources>
**<Style x:Key="a" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="50"/>
<Setter Property="Background" Value="Indigo"/>
</Style>
<Style x:Key="b" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="16"/>
</Style>
<Style x:Key="c" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="TimesNewRoman" />
<Setter Property="FontSize" Value="50"/>
<Setter Property="Background" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBlock Margin="26,41,39,0" Style="{StaticResource a}" Height="100" VerticalAlignment="Top">TextBlock with Style1</TextBlock>
<TextBlock Margin="26,77,39,0" Height="32" VerticalAlignment="Top">TextBlock with no Style</TextBlock>
<TextBlock Margin="26,105,67,96" Style="{StaticResource b}">TextBlock with Style2</TextBlock>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="26,170,-26,0">
<Button Style="{StaticResource c}">
<Bold >Styles</Bold></Button>
<Button Style="{StaticResource c}">are</Button>
<Button Style="{StaticResource c}">cool</Button>
</StackPanel>
</Grid>
here i declaring the style for both textBlock and button.Use this one..

WPF UserControl Style

I want to set the background property of all the usercontrols of my project.
I tried with
<style TargetType={x:Type UserControl}>
<setter property="Background" Value="Red" />
</style>
It compiles but didn't work.
¿Any Idea?
Thanks!
You can only set a a style to a specific class, so this will work (create a UserControl object, not very useful):
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<UserControl Name="control" Content="content"></UserControl>
</Grid>
But this doesn't (Create a class derived from UserControl):
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content"></l:MyUserControl>
</Grid>
What you can do is either explicitly set the style using the Style property:
<Window.Resources>
<Style TargetType="{x:Type UserControl}" x:Key="UCStyle">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content" Style="{StaticResource UCStyle}"></l:MyUserControl>
</Grid>
or create a style for each derived class, you can use BasedOn to avoid duplicating the style content:
<Window.Resources>
<Style TargetType="{x:Type UserControl}" x:Key="UCStyle">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type l:MyUserControl}" BasedOn="{StaticResource UCStyle}" />
</Window.Resources>
<Grid>
<l:MyUserControl Name="control" Content="content"></l:MyUserControl>
</Grid>
I think you're missing some double quotes:
Try this:
<Window.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<UserControl Name="control" Content="content"></UserControl>
</Grid>

Resources