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>
Related
I am trying to extend an implicit Style of a control which is defined in any parent control within a custom UserControl. This doesn't seem to work as I would expect.
The scenario would be: Including a ResourceDictionary in MainWindow.xaml which defines a default theme. Than I want to extend a base Style which is defined in that theme within my UserControl.
What I have already tried:
Having a MainWindow.xaml which defines the following DefaultStyle in its Resources:
<Window x:Class="UserControl_Style_Inheritance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UserControl_Style_Inheritance"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"></Setter>
</Style>
</Window.Resources>
<Grid>
<local:MyUserControl></local:MyUserControl>
</Grid>
</Window>
And having a MyUserControl.xaml which should extend the defined DefaultStyle of MainWindow.xaml:
<UserControl x:Class="UserControl_Style_Inheritance.MyUserControl"
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:UserControl_Style_Inheritance"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button>Hello</Button>
</Grid>
</UserControl>
But MyUserControl is based on the normal Button Style by having a gray background instead of a blue background of the Style defined in MainWindow.xaml.
What am I doing wrong, or where am I thinking into the wrong direction?
Edit: Putting the default Style into the Application.xaml file works, but this is not what I want to do.
Try adding the button style to App.xaml:
<Application.Resources>
<Style x:Key="BtnStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Blue"></Setter>
</Style>
</Application.Resources>
And use it in your UserControl like this:
<UserControl.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BtnStyle}">
<Setter Property="BorderBrush" Value="Red"></Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button>Hello</Button>
</Grid>
I am trying to change the font-size globally. For this, I added styles in app.xaml. Here my FontSz property is in MainWindowViewModel. Is there any way to make this binding possible?
<Application.Resources>
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontSize" Value="{Binding Path=???.FontSz}" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"/>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"/>
</Application.Resources>
You will need to use DynamicResouce for this. Add the system namespace as below
xmlns:system="clr-namespace:System;assembly=mscorlib"
<Application.Resources>
<system:Double x:Key="FontSz">20</system:Double>
<Style x:Key="baseStyle"
TargetType="{x:Type Control}">
<Setter Property="FontSize"
Value="{DynamicResource FontSz}"/>
</Style>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource baseStyle}"/>
<Style TargetType="{x:Type Label}"
BasedOn="{StaticResource baseStyle}"/>
</Application.Resources>
MainWindowViewModel
In your command execution, add the following code:
Application.Current.Resources["FontSz"] = 18d;
You can change the fontsize from 18d to the fontsize selected by the user in your MainViewModel.
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'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 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}"/>