ResourceDictionary and Styles not merging across application - wpf

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}">

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?

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>

VSTO Office (Excel) Add-In - WPF XAML Style, ResourceDictionary

I want to deploy my VSTO Office Excel-Add-In with the Windows Installer.
I created the Installer and installed the Add-In on a Virtual PC, to test it.
Now i have the Problem, that the Styles not working, but if i debug or run it in Visual Studio it does work.
For example, i created a Style like this:
<Style TargetType="{x:Type Button}">
<Style.Setters>
<Setter Property="Background" Value="Snow" />
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="5" />
</Style.Setters>
</Style>
Now i merge the ResourceDictionary (with the style in it) with the ResourceDictionary of the Window:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/assembly;component/UI/Resources/Style.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
It does only works after the Setup, when i use Keys for the Styles and set the Style directly to the Control.
This is the ResourceDictionary with the Styles (Styles.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Button}">
<Style.Setters>
<Setter Property="Background" Value="Snow" />
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="5" />
</Style.Setters>
</Style>
</ResourceDictionary>
And here is the "Merge"-ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ExcelAddIn;component/UI/Resources/Style/Brushes.xaml" />
<ResourceDictionary Source="/ExcelAddIn;component/UI/Resources/Style/ControlTemplates.xaml" />
<ResourceDictionary Source="/ExcelAddIn;component/UI/Resources/Style/Styles.xaml" />
<ResourceDictionary Source="/ExcelAddIn;component/UI/Resources/Style/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
To merge the ResourceDictionary to the Window-Resources, i tried to used:
These are working when i debug/run it with Visual Studio but not after Setup:
<ResourceDictionary Source="/ExcelAddIn;component/UI/Resources/Style/Skin.xaml" />
<ResourceDictionary Source="pack://application:,,,/ExcelAddIn;component/UI/Resources/Style/Skin.xaml" />
<ResourceDictionary Source="pack://application:,,,/ExcelAddIn;v1.0.0.0;component/UI/Resources/Style/Skin.xaml" />
These are generally not working:
<ResourceDictionary Source="pack://application:,,,/UI/Resources/Style/Skin.xaml" />
<ResourceDictionary Source="/UI/Resources/Style/Skin.xaml" />
I think the uri in the Source property is the problem. Try using Pack URIs

Implicit Style for application

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>

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