Implicit Style for application - silverlight

Implicit Style not working in App.xaml, but is working with local page resources. How do I make a global style for a control?
<navigation:Page.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
</navigation:Page.Resources>

In App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StilDict.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In StilDict.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
,,,, and what you use else>
<Style x:Key="ButtonStyleNavyOnRed" TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
using style anywhere on your project
<UserControl>
<Button Style={StaticResource ButtonStyleNavyOnRed} Content="Yahoo! :)"/>
</UserControl>
Important Note if you delete x:Key="ButtonStyleNavyOnRed" part all of your Target types get this style, but not Button derived objects. http://msdn.microsoft.com/en-us/library/system.windows.style(v=vs.95).aspx
Hope Helps!

This works for buttons!
<Style TargetType="ButtonBase">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>

Related

Wpf Style is only applied in the editor view

As I wanted to define a default text style for a Textblock, I created a ResourceDictionary
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource DefaultFontLight}" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Foreground" Value="Black" />
</Style>
which is referenced in the App.xaml
<Application.Resources>
<ResourceDictionary>
<FontFamily x:Key="DefaultFontLight"> path to font </FontFamily>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In the editor view, the style is applied properly, but when I run the application in debug/release mode, is uses some other default font style and not the one I specified.
EDIT: I found out, that the problem is StaticResource DefaultFontLight, which seems not to be found in the ResourceDictionary Styles.xaml even though this Resource exists. If I replace the StaticResource with the actual path, everything works. Also, if I copy the style in the App.xaml it works too
<Application.Resources>
<ResourceDictionary>
<FontFamily x:Key="DefaultFontLight"> path to font </FontFamily>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource DefaultFontLight}" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Foreground" Value="Black" />
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Any ideas why this is the case?

Use style in multiple usercontrols

In my main control i defined
<Control.Resources>
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
</Control.Resources>
How can i use this style for textBoxes in another user controls that placed inside my main control?
I dont need to apply style to ALL textboxes, but i want style that can be reused in any control i want. Is it possible without custom controls?
You should add resources dictionary to your project.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
Then add reference to this dictionary in app resources:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="YOur class"
StartupUri="StartupWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\TextInputStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
So, you can use your style in any part of your project
Move your style up to the window resources
<Window.Resources>
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
Then refer to the style like this:
<TextBox Style="{StaticResource TextInput}" />

Change Background in MetroWindow (MahApp)

How can I change the background of a MetroWindow?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Behaviours="clr-namespace:MahApps.Metro.Behaviours"
xmlns:Converters="clr-namespace:MahApps.Metro.Converters"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Style BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" TargetType="Controls:MetroWindow">
<Setter Property="Background" Value="LightGray" />
<Setter Property="BorderBrush" Value="#FFB9B9B9" />
<Setter Property="BorderThickness" Value="0,1,0,0" />
</Style>
create a style with a key (and put the style in your App.xaml or in a resource dictionary and put this in your App.xaml)
<Style x:Key="CustomMetroWindowStyle" TargetType="{x:Type Controls:MetroWindow}">
<Setter Property="Background"
Value="LightGray" />
<Setter Property="BorderBrush"
Value="#FFB9B9B9" />
<Setter Property="BorderThickness"
Value="0,1,0,0" />
</Style>
and use it like this
<Controls:MetroWindow x:Class="MetroDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{DynamicResource CustomMetroWindowStyle}">
</Controls:MetroWindow>

ResourceDictionary and Styles not merging across application

I have an application that uses a ResourceDictionary to set the styles, which it does nicely. However, the font is a little small and I would like to change that but the resource directory is from a .dll so I can't edit it.
As you will notice I'm just starting out with dictionaries.
I thought I could override this by using MergedDictionaries and just add a style to override it:
<Application x:Class="IDIUserInterface.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Windows/WindowMain.xaml" >
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ReuxablesLegacy;component/mercury.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Window">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
<Style TargetType="Page">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
</ResourceDictionary>
</Application.Resources>
To my shock this actually worked, but only in the design view. As soon as I compiled the code and ran the application the fonts return to their former size.
Is there a reason for this or am I doing something wrong?
Thanks in advance,
SumGuy
If anyone interested I solved it with:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ReuxablesLegacy;component/mercury.xaml" />
<ResourceDictionary>
<Style x:Key="MyWindowStyle" TargetType="Window">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Background" Value="WhiteSmoke" />
</Style>
<Style x:Key="MyPageStyle" TargetType="Page">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Background" Value="WhiteSmoke" />
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
And then added
Style="{StaticResource MyWindowStyle}"
...into the window header (or substitute Page for pages)
Change the following:
<Style TargetType="Window">
<Style TargetType="Page">
To
<Style TargetType="{x:Type Window}">
<Style TargetType="{x:Type Page}">

How to apply a WPF style to all elements of a particular type?

<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Times New Roman" />
<Setter Property="FontSize" Value="30" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="#FFCA5132" />
</Style>
</Window.Resources>
This aboved code looks like the right selection to apply WPF style to all buttons in a specific window. But I want to apply this style for all buttons in my program.
I think I need to write down this code in the <Application.Resources></Application.Resources>. But it doesn't run. How can I do that?
<Application.Resources> is a ResourceDictionary. You can't add to it both a ResourceDictionary and a Style, like you are doing in your code. Place the Style inside the ResourceDictionary, like this:
<Application.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero
, Version=3.0.0.0,Culture=neutral
, PublicKeyToken=31bf3856ad364e35
, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style\AppStyle.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="meiryo" />
<Setter Property="FontSize" Value="12" />
</Style>
</ResourceDictionary>
</Application.Resources>

Resources