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>
Related
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 have created a user control class library and I used a ResourceDictionary file in it.
Now, I want to use my usercontrol in a WPF application, but I have to add ResourceDictionary file again in my projet! If I don't add it, it brings the ResourceDictionary file, and show an error on MergeDictionaries block!
Am I missing something!?
Resource dictionary is:
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
<Rectangle Fill="Transparent" Cursor="Hand"/>
</ControlTemplate>
<Style x:Key="ItemStyle" TargetType="ContentControl">
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualWidth}"/>
<Setter Property="MinHeight" Value="60"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Content" Value="MyTextBox"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<s:MoveThumb Template="{DynamicResource MoveThumbTemplate}"/>
<ContentPresenter Name="MainControl" Content="{TemplateBinding ContentControl.Content}"
Margin="5,0,10,0"/>
<Grid Opacity="0" Margin="-3">
<s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
adding to user control:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/MoveResizeThumb.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Give this a try:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Just to clearify the answer of #Willem: when having additional resources after the merged dictionary, there may appear the error "x:key attribute is required". In this case, those resources have to be inside the resource dictionary:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary source="/assembly;component/resource.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBlock" BasedOn="{StaticResource SetupTextBlockStyle}" />
<ResourceDictionary>
</UserControl.Resources>
<Grid />
In response to #ThisHandleNotInUse and #OliverAssad comments in the accepted answer.
In case of x:Key attribute required error, the ResourceDictionary tag should be modified as follows:
<UserControl.Resources x:Key="myKey">
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Another way is to add the resource at an application level
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ProjectStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
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 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>
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}"/>