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?
Related
I declare the following ResourceDictionary, TargetType="TextBlock" in the App.xaml file:
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
Later, after the application has started, I want to be able to on-demand load another resource file (MyCustomResources.xaml) that has a style declaration, again with TargetType="TextBlock", but with ONLY one Setter that declares a Forground color of "Green".
The "Green" Foreground color will need to globally override the original Foreground "Red".
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>
For this application, overriding the Foreground color for a TextBlock in a local view xaml file is not acceptable.
I want to still keep and use the other two global Setter properties for FontStyle and FontSize for TextBlock controls declared in the App.xaml file, without having to declare them again in the MyCustomResources.xaml file.
Is it possible to do what I am describing here, or some other way?
If you use merged dictionaries, I believe the order they are added to the collection is used to determine precedence.
So if you're app.xaml is defined as so:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/DefaultDict.xaml" />
<ResourceDictionary Source="/Override.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBlock" x:Key="baseStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="20" />
</Style>
</ResourceDictionary>
</Application.Resources>
Then in DefaultDict.xaml you simply create an implicit style which all textblocks will use until the override file is loaded.
<Style TargetType="TextBlock" BasedOn="{StaticResource baseStyle}">
</Style>
And your Override.xaml you can override any setters.
<Style TargetType="TextBlock" BasedOn="{StaticResource baseStyle}">
<Setter Property="Foreground" Value="Pink" />
</Style>
You should be able to add and remove your override file at runtime and it should update correctly
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 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>
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}">
<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>