In my App.xaml I have the following code:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- primary color -->
<ResourceDictionary>
<!-- include your primary palette -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Scroll.xaml" />
<ResourceDictionary Source="Styles/ComboBox.xaml" />
<ResourceDictionary Source="Styles/ComboBoxCanType.xaml" />
<ResourceDictionary Source="Styles/ComboBoxCanType1.xaml" />
<ResourceDictionary Source="Styles/ComboBox1.xaml" />
<ResourceDictionary Source="Styles/TextBox.xaml" />
<ResourceDictionary Source="Styles/TaskContextMenu.xaml" />
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.red.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#E42F34"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#FFFFFF"/>
<!--<LinearGradientBrush x:Key="PrimaryHueMidBrush" EndPoint="1.5,1.5" StartPoint="0.5,0">
<GradientStop Color="#8E2E2F" Offset="0"/>
<GradientStop Color="#E43D47" Offset="1"/>
</LinearGradientBrush>-->
<!--#F14450-->
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#F14450"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#4D1DCF"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I want to have a specific control from the HandyControl UI (specifically the WaveProgressBar) and therefore I need these lines:
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
For it to work and load the control. However, if I do do this, it messes with my MaterialDesign controls and other default controls I have, so how can I set the theme for ONLY the WaveProgressBar? Any help would be appreciated!
Resources have scope.
You can merge those just for your control:
<WaveProgressBar ......>
<WaveProgressBar.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
Or as resources of a grid containing several of these controls.
Or as resources of a usercontrol containing a number of these controls.
And so on.
Having said that.
Styles and templates can be applied targetting a specific control and you seem to have a unique control there.
So you could grab the bits you need and put them in a style which sets template, property values etc just to WaveProgressBar. This would also likely be more efficient.
Related
I want to use TabControl with MaterialDesignExtensions.I'm getting an error in the app.xaml. What's the point I'm missing?
Load Nugets:
MaterialDesignColors v1.2.7
MaterialDesignThemes.Wpf v3.2.0
MaterialDesignExtensions v3.2.0
App.xaml
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
xmlns:culture="clr-namespace:MyApp.Cultures"
xmlns:local="clr-namespace:MyApp"
xmlns:controls="clr-namespace:MaterialDesignExtensions.Controls;assembly=MaterialDesignExtensions"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/MaterialDesignDarkTheme.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/Generic.xaml" />
<!-- primary colors -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#244886"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary600}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary600Foreground}"/>
</ResourceDictionary>
<!-- accent color -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent400}"/>
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent400Foreground}"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Error Message
An error occurred while finding the resource dictionary "pack://application:,,,/MaterialDesignExtension:component/Themes/Generic.xaml"
I found this workaround, just include directly the TabControl dictionary from the MaterialDesignExtension 3.2.0
<ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/TabControlTemplates.xaml" />
I have this in Brushes.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SkinBox.Controls">
<SolidColorBrush x:Key="{x:Static controls:Keys.BackgroundBrushKey}"
Color="{DynamicResource {x:Static controls:Keys.BackgroundColorKey}}" />
</ResourceDictionary>
And use it like this in Generic.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SkinBox.Controls"
xmlns:system="clr-namespace:System;assembly=System">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Skins/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
The problem is that wpf freezes the brush so the DynamicResource has no effect.
Is there a clean way to solve this? I can only think of nasty hacks.
Thought of a workaround:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SkinBox.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Skins/Yellow.Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
This still does not give true dynamic resources but it reapplies the brushes so that they update when applying the skin.
Removes the need for duplicating the brushes in every skin.
<LinearGradientBrush x:Key="MaroonGradientBrush" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF0C0B0B" Offset="1"/>
<GradientStop Color="#FFBF5656"/>
</LinearGradientBrush>
<Window
x:Class="GraphViewerWindow"
RenderOptions.EdgeMode="Unspecified"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DaedalusGraphViewer="clr-namespace:DaedalusGraphViewer"
Title="Window1" Height="900" Width="900">
<TextBox Background="{StaticResource MaroonGradientBrush}" />
</Window>
the program opens up with a window with the correct gradient brush. However, the design view still doesn't load the window because it can't find maroongradientbrush.
Edit:
Found my problem. It is this exactly:
How to move App.xaml and not to break designer?
but there isn't a posted solution to it
Your XAML is wrong. That's why it's not working
Put back these into App.xaml in the following manner:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionaries/GraphViewerBrushes.xaml" />
<ResourceDictionary Source="ResourceDictionaries/GraphViewerTreeViewResources.xaml" />
<ResourceDictionary Source="ResourceDictionaries/SignalScrollViewerResources.xaml" />
<ResourceDictionary Source="ResourceDictionaries/ValidationErrorResources.xaml" />
<ResourceDictionary Source="ResourceDictionaries/GraphViewerToolbarResources.xaml" />
<ResourceDictionary Source="ResourceDictionaries/SavedResourcesIMightUse.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If you put all these resources into EACH UserControl of your app, you will create a horrendous memory hog.
StaticResources should work just fine. I do it this way in my projects and never had any problems. even if your resources are stored in external assemblies (pack://applicaton, etc)
File app.xaml
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="appBrush" Color="Red"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
File Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="ct">
<Rectangle Fill="{StaticResource appBrush }"/>
</ControlTemplate>
</ResourceDictionary>
Then MainWindow uses the template defined.
<Control Template="{StaticResource ct}"/>
Run. It says cannot find appBrush. What's the problem?
Note, changing StaticResource to DynamicResource definitely lets it work. But what is the reason forcing me to make the change?
Change your App.xaml to this and it will work:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="appBrush" Color="Red"/>
</ResourceDictionary>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I believe it is related to how resources are merged together. In these type of situations, I always use a Brushes.xaml resource dictionary to define all my brushes, and merge that in ahead of any other resource dictionaries.
This is probably a really stupid question but I can't figure this out.
I have a page with a MergeDictionary defined:
<navigation:Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</navigation:Page.Resources>
and I reference the styles in TourneySetupStyles.xaml in my XAML like this with no problem:
<TextBlock Text="Tourney Name:" Style="{StaticResource TourneySetupTextStyle}" />
However, now I need to add another page resource like this:
But the designer now throws a warning:
"The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection."
So I add a key to my ResourceDictionary like this:
<navigation:Page.Resources>
<local:Tournament x:Key="tournament" />
<ResourceDictionary x:Key="whatever">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</navigation:Page.Resources>
and the warning goes away. BUT now my reference to the style in TourneySetupStyles no longer works:
"Cannot find a Resource with the Name/Key TourneySetupTextStyle"
So I guess the question is: How do I access the style now that the ResourceDictionary is keyed?
I just ran into this today -- I'm cross compiling to WPF / Silverlight.
Put the local resource inside the ResourceDictionary node, don't put a x:Key on the ResourceDictionary node.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/mydll;component/folder/MyResDict.xaml" />
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush x:Key="OrangeGradient"
EndPoint="0.5,1"
MappingMode="RelativeToBoundingBox"
StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<RotateTransform CenterY="0.5"
CenterX="0.5" />
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FFF3801E" />
<GradientStop Color="#FFEDB17E"
Offset="0.5" />
<GradientStop Color="#FFF3801E"
Offset="1" />
</LinearGradientBrush>
</ResourceDictionary>
</UserControl.Resources>
I can't explain why - but I know it works...
hth
sigh it seems that the order of the declarations is important, as soon as I move the first resource down it now works:
<navigation:Page.Resources>
<ResourceDictionary x:Key="TourneySetupStyles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<local:Tournament x:Key="tourneySetupViewModel" />
</navigation:Page.Resources>
If anyone can explain why it would be great for future reference...
I came across the same problem.
I resolved the issue be defining my dictionary merges within the xaml file of the application instead of the view itself.
Ex:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\Brushes\Brushes_Dictionary.xaml" />
<ResourceDictionary Source="Resources\Storyboards\Storyboard_Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Yes, I've just been bitten by this. As soon as Silverlight loads the merged resource dictionary, it deletes all local resources you've already added! In my case I need to programmatically add a resource before the InitalizeComponent() call, but since the UserControl includes a merged ResourceDictionary that resource is lost. IMHO this is a bug in Silverlight.
But putting local resources after the ResourceDictionary will work for cases like yours so I've up voted it.