I want to use the Aero textbox styling, but still override some properties. I try to accomplish this by:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
</ResourceDictionary>
However, this results in a StackOverflowException when starting my app.
When I remove the reference to PresentationFramework.Aero, this works but I get the default OS styling, which makes the app ugly. ;)
So, in effect: if I want to override some style on all my textboxes I cannot get the Aero look. If I want the Aero look, I cannot override any styling. Deadlock.
Any way to solve this?
It seems to work if you put the Style as a lower-level resource, instead of in the same ResourceDictionary:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<Border BorderBrush="Blue" BorderThickness="3">
<Border.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
</Border.Resources>
<TextBox />
</Border>
</Grid>
Unlike the code in accepted answer this one allows using resource dictionary for styles. Shamelessly stolen from http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/ answered by Vivien Ruitz
<!--App.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/>
<ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/>
...
</ResourceDictionary.MergedDictionaries>
<!--StyleDictionary.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >
... <!--Extend the aero style here-->
</Style>
<!--ApplyStyleDictionary.xaml-->
<Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/>
Related
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?
I have a Wpf Application with the following structure
MainSolution -> Views (folder) -> NextGenDG (WPF Project) ->DisplayResources(folder) -> BRUserControlStyles.xaml
Here is BRUserControlStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NextGenDG.DisplayResources">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRColors.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRFonts.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRButtonStyles.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRCommonControlStyles.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRToggleButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style
x:Key="UserControlDarkBackgroundStyle"
TargetType="{x:Type UserControl}"
BasedOn="{StaticResource {x:Type UserControl}}">
<Setter
Property="Margin"
Value="0" />
<Setter
Property="Padding"
Value="0" />
<Setter
Property="Background"
Value="Red" />
<Style.Resources>
<Style
TargetType="{x:Type Button}"
BasedOn="{StaticResource ButtonLightTextGradientDarkBackgroundStyle}" />
<Style
TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource TextBlockLightTextStyle}" />
<Style
TargetType="{x:Type ToggleButton}"
BasedOn="{StaticResource ToggleButtonLightTextGradientDarkBackgroundStyle}" />
<Style
TargetType="{x:Type Separator}"
BasedOn="{StaticResource SeparatorDarkLineStyle}" />
</Style.Resources>
</Style>
</ResourceDictionary>
Now I am trying to use the style inside a UserControl that is inside a folder called UserControls as follows. This UserControl is being called in the MainWindow
<UserControl x:Class="NextGenDG.UserControls.NextGenHeaderCartridgeStatusUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NextGenDG.UserControls"
mc:Ignorable="d"
Style="{DynamicResource UserControlDarkBackgroundStyle}"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Grid.Column="1"
Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</UserControl>
Problem is if I say StaticResource UserControlDarkBackgroundStyle it complains saying the resource is not found. So when I use DynamicResource UserControlDarkBackgroundStyle it dosen't apply the style. Please help. At a glace is my ResourceDictionary path correct?
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
I'm using MahApps.Metro to achieve Metro UI in my app.
I have a listview and MahApps.Metro is changing style for it. MahApps styles for listview are here.
Loading of styles:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="pack://application:,,,/FineRSS;component/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
I need to keep track of selected listviewitems so I'm using the next approach:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListView.ItemContainerStyle>
But MahApps.Metro's style is overwritten to ListView's default.
What can I do to keep either styles and IsSelected binding?
I'm not positive I follow what you're trying to do, but would it make sense to make your Style be BasedOn the default one that is loaded?
Something like
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}"
BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListView.ItemContainerStyle>
I use Style.Triggers & DataTrigger to Enable/Disable a button.
But the style I've applied is gone. It is the Expression Dark.
Is it possible to fix it and return style to the button?
Thanks!!!
<Application.Resources>
<ResourceDictionary x:Uid="ResourceDictionary_1" >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Uid="ResourceDictionary_3" Source="/Themes/ExpressionDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
SOLUTION:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" >
In your Style try something like this:
<Button ....>
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
....
</Style>
</Button.Style>
</Button>