I have a resource dictionary in a class library that has a bunch of SolidColorBrush's and Control style's. It works in the designer when i make sure that all the controls whose style i am setting are using DynamicResource's but if i switch it to use a StaticResource either the designer breaks or my application fails to run... or both.
This works
<Image Source="{Binding Image}" Style="{DynamicResource DriveImageStyle}"/>
This breaks
<Image Source="{Binding Image}" Style="{StaticResource DriveImageStyle}"/>
giving me errors such as:
XamlParseException: Provide value on
'System.Windows.Markup.StaticResourceHolder' threw an exception.
Cannot find resource named 'DriveImageStyle'
When using resources in the resource dictionaries themselves (setting the background colour of a style)
This works
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundColour}" />
This breaks
<Setter Property="Background" Value="{StaticResource ButtonBackgroundColour}" />
and gives me errors in the designer such as:
Exception thrown: 'System.Windows.Markup.XamlParseException' in
PresentationFramework.dll
Additional information: '{DependencyProperty.UnsetValue}' is not a
valid value for property 'Background'.
Can anyone give me a reason as to why it acts in this way?
Additional information
In my View's I reference the dictionary like this:
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</UserControl.Resources>
And I merge them in my class library like so:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colours.xaml"/>
<ResourceDictionary Source="Buttons.xaml"/>
<ResourceDictionary Source="Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
I wish I had time to go test but I want to say this is how I did it last time I was using directly from a usercontrol. However I'd generally advise against it and just slap it over in the app.xaml of the proj or someplace more centralized unless that one view is genuinely the only one need it. Either way give this a shot instead, they can be finnicky about details.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Related
I'm building a library (plugin for Revit). I have a Window in which I have included Material design successfully.
When I try adding TextBox control to this Window, I get the following error
System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'
Inner Exception: NotImplementedException: The method or operation is not implemented.
Since I'm building a library, I don't have an App.xaml file, so I created a resource dictionary with the following content:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I include it to my Window using
<Window.Resources>
<ResourceDictionary Source="/MyAsembly;component/MaterialDesign.xaml" />
</Window.Resources>
As I said, using plain simple TextBox throw an exception, no styles, no anything
<TextBox />
All the other WPF controls that I've used so far work normally after including Material design in a window/page.
I have another Window in the same app where I already have TextBox. If I try including MaterialDesign to that Window, I get the same error; without MaterialDesing, TextBox works normally. If I include MaterialDesing and comment out the TextBox, the code works normally.
Any help is highly appreciated.
EDIT: Demo app that demonstrates the problem can be found here.
You must also provide the XAML namespace along with your ResourceDictionary declaration as shown below.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
put this line to your main window tag
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Suppose I have App.xaml with following content:
[...]
<Application.Resources>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="BorderBrush" Value="{StaticResource Theme.CheckBox.Border}" />
<Setter Property="Foreground" Value="{StaticResource Theme.Foreground}" />
</Style>
</Application.Resources>
[...]
Now, I want to move those definitions to external assembly. This way I can reuse WPF controls and window styles across my applications. For example I could create NuGet package and install those styles in seconds.
You can create a simple classLibrary project, Define all your styles and templates etc, in separated resource dictionaries (recommended) and merge all those dictionaries into one dictionary.
To use those styles from that specific assembly just refer to it in your App.Xaml file,
The following shots should gave you a global view
I am used to create a separate ResourceDictionary for each style (for easy access), and all those resource dictionaries are merged in the ResourceLibrary.Xaml
Now to refere to that ControlLibraryAssembly (after adding a reference to it), in your app.xaml file add
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ControlLibrary;component/ResourcesDictionaries/ResourceLibrary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You need to make resource assembly. Here you can read how you can create it:
https://msdn.microsoft.com/en-us/library/aa984332(v=vs.71).aspx
I've creat a ResourceDictionary named Dictionary1.xaml, here is code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Red">
</Setter>
</Style>
I ref it in App.xaml or MainWindow.xaml like this:
<Application.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>
<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Window.Resources>
And in View Designer, window's background changes to red, but when the application is running, it's background is default(white), why? How to do it ?
you can set other controls' style in this way,except Window! try to set sytle for Button,Lable and so on, you will get a correct result. But for a Window, you will not.
see my another answer ,it may help you: How to add a common control on all my Windows?
you must set the Style's x:Key and set the Window's style explicitly:
Style="{DynamicResource key_name}"
why the View Designer shows a correct result? it may be a bug. vs2012/13's xaml Designer has many bugs, you can search or commit in msdn.I've commit one in it :
https://connect.microsoft.com/VisualStudio/feedbackdetail/view/925324/multibinding-report-an-issue-on-latest-vs-xaml-editor
but ms closes it and they will not repaire it recently.
How can WPF resources - including styles, templates, etc. - be organized, so that I can use them across Windows, Pages or even Projects. What options do I have to achieve maximum re-usability of my resources and a maintainable structure (for example one file per Template)?
For example: I am creating a WPF application and I want to use a TabControl, but I want to make major changes to it. So I could create a style in and apply it to the TabControl and TabItem. That's ok, but where can I place my resources to keep my Window XAML clear and have the style accessible from other Windows or projects as well?
I found that I can add it to App.xaml but that is only a solution for one project and allows sharing just between items of this project. Also, I think it would be better to have these templates a little separate from other code, than placing it all in some page or app.xaml?
I usually create a seperate styling project, which I reference from the projects, which I want to style. The styling project has a fixed structure like this:
For every control, I create a styling ResourceDictionary. For example for my buttons:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
</Style>
<Style x:Key="ToolbarButton" TargetType="Button">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Transparent"></Setter>
</Style>
</ResourceDictionary>
In one main ResourceDictionary, I merge all the other dictionaries, in this case in the file IncaDesign.xaml, which you can see in the picture above:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Commons.Controls;assembly=Commons">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Converter/Converter.xaml" />
<ResourceDictionary Source="Styles/Button.xaml" />
<ResourceDictionary Source="BitmapGraphics/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Default Styles -->
<Style TargetType="Button" BasedOn="{StaticResource PrimaryButtonStyle}"></Style>
</ResourceDictionary>
Notice how I defined the default styles, which are applied automatically, unless you specify otherwise. In every window or control, that you want to style, you only need to reference this one ResourceDictionary. Note the definition of the source, which is a reference to the assembly (/Commons.Styling;component...)
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Commons.Styling;component/IncaDesign.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Default styles will be set automatically now, and if you want to access a resource explicitly, you can do this, using StaticResource.
<Viewbox Height="16" Width="16" Margin="0,0,10,0">
<ContentControl Content="{StaticResource FileIcon32}" />
</Viewbox>
This is very nice solution in my opinion, which works for very complex solutions, including modular solutions, for example built with PRISM.
A ResourceDictionary is for what you are looking.
Here is an explanation for how to use them within and across projects.
You can create a project that contains Resource Dictionaries, either in your solution or in a separate one. Your project will be a Class Library type and can then easily be .dll referenced from any other project. Here is an article describing this: Resource Dictionary Article
As I have multiple Windows in my application, I am looking for a solution that does not require me to set a binding on each Window.
I created a ResourceDictionary which has a style for the Window Background:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="AliceBlue"/>
</Style>
In my XAML, I set the ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Templates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
There is no error, but my Window color stays white.
This appears to be caused by a combination of the order in which WPF loads/processes styles from nested ResourceDictionary, and the specifics of the Window class.
Assume MainWindow is defined as per your post. Now put the following in Templates.xaml:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type Window}" x:Key="myStyle">
<Setter Property="Background" Value="Green"/>
</Style>
If MainWindow has no style defined, then you will see that in the designer it appears with a red background. The designer is parsing the whole Xaml and loading the resource dictionary, and then drawing the results. The style is read before the window is drawn, and so the red background is applied.
When you run the application, the window is created before the ResourceDictionary is applied. It looks for a default style (a style with x:Key="{x:Type Window}") before the nested ResourceDictionary is processed, and finds nothing. Therefore at runtime, the window appears with default colour. (This is the behaviour described in the comments above.) Remember that the style with x:Key="{x:Type Window}" has a default value that matches the Windows style.
This is borne out if you use myStyle explicitly. If you add to your Window definition the attribute Style="{StaticResource myStyle}" you'll find that the designer fails, but you also get a run-time error, because myStyle hasn't been created at the time that the Window needs it. If you switch to Style="{DynamicResource myStyle}" then you'll see that it works as you hope, because DynamicResource will update once the ResourceDictionary has been parsed and the style included.
So, applying this, you can fix the problem in one way by adding this to your Window element: Style="{DynamicResource {x:Type Window}}" - but this is cludgy. The better solution is to include your resource dictionary in the app.xaml file, where it will be parsed before any window is opened and thus available to all:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Templates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The real issue here is that your Window is not really a Window: it is a class that derives from Window and will in fact be MainWindow, Window2, etc... This means that the automatic style wireup for a Window will never work in this way, and some level of manual binding will unfortunately always be required.
This is the solution I used in my application. It lets me keep all my window styles together, and requires just a couple lines after the <Window.Resources> section.
Do your Style like so:
<Style x:Key="MyWindowStyle">
<Setter Property="Window.Background" Value="AliceBlue"/>
</Style>
Then, in your Window, after </Window.Resources> include the following:
<Window.Style>
<Style BasedOn="{StaticResource MyWindowStyle}"/>
</Window.Style>
Add a new brush in your resource dictionary
<SolidColorBrush x:Key="WindowBackground" Color="AliceBlue" />
and in your WPF window simply set the required resource to the window background property
<Window x:Class="GDD.Presentation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300"
Background="{StaticResource WindowBackground}">