In WPF, this used to work fine:
<Page.Resources>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
but adding a converter (see below) causes an error on the 2nd resource (Style.xaml): Each dictionary entry must have an associated key.
<Page.Resources>
<local:MySizeConverter x:Key="sizeConverter"/>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
However, adding a key to the 2nd line (e.g. <ResourceDictionary x:Key="myStyleDict" Source="resources/Styles.xaml" /> causes the following error in code behind
The name 'aTextBlockUsedToWork' does not exist in the current context
where aTextBlockUsedToWork could be successfully accessed in code behind before adding the key. Note that the converter works fine if I comment out the style resource. How can I have both of the resources working?
You need to use MergedDictionaries to import another dictionary file, like this:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="resources/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:MySizeConverter x:Key="sizeConverter"/>
</ResourceDictionary>
</Page.Resources>
Related
New to the WPF process here and trying to define a merged dictionary of a collection of merged dictionaries that are maintained in a utility DLL. I am attempting to resolve the resource location error.
Error
IOException: Cannot locate resource 'ui/wpf/imagelist16x16.xaml'
Theme.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/UI/WPF/ImageList16x16.xaml" />
<!--Future xaml file references-->
</ResourceDictionary.MergedDictionaries>
<Style x:Key="EmptyStyle"/>
</ResourceDictionary>
ImageList16x16.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clr="clr-namespace:System;assembly=mscorlib">
<BitmapImage x:Key="Accept" UriSource="UI/Resources/Images16x16/accept.png"/>
</ResourceDictionary>
User Control Resource Reference
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BHP_Utilities;component/UI/WPF/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
When executed, the Theme.xaml is found as the error occurs based on the dictionary reference within it. The build option for both xaml files is Page and the Custom Tool property is set to XamlIntelliSenseFileGenerator.
Since the files are in the same folder, could you try removing "/UI/WPF/"
<ResourceDictionary Source="ImageList16x16.xaml" />
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"
I have list of icon which will be displayed for different button i.e. save delete.
what I want to do is to list down all the icons in some xaml file like app.xaml
<Resource x:Key="error" Source="Icons/Error.ico" />
<Resource x:Key="save" Source="Icons/save.ico" />
then want to access same thing in individual file as follow.
Icon="{Binding save}"
I would appreciate if someone suggest me correct approach if this is not correct.
Create resource dictionary Images.xaml
Add all images to that dictionary in this form
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="Icon1"
UriSource="Images/icon1.png" />
.....
</ResourceDictionary>
When you want to use it,
<Image Source={StaticResource Icon1} />
Dont forgot to include that Image.xaml, to the place where you want to use it... actually you can merge it directly to your main dictionary in App.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Images.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I have a local resource in XAML and I want it to be used by other parts of the code. How can I make it "global" (i.e. an application-wide resource)? Here is my local resource:
<ResourceDictionary >
<local:BoolToLightConvertor x:Key="LightConverter" / >
</ResourceDictionary>
How can I put this in the App.xaml?
Application Resources
In addition to defining resources at the level of the element or Window, you can define
resources that are accessible by all objects in a particular application. You can create an application
resource by opening the App.xaml file (for C# projects) or the Application.xaml file
(for Visual Basic projects) and adding the resource to the Application.Resources collection, as
shown here:
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<SolidColorBrush x:Key="appBrush" Color="LightConverter" />
</Application.Resources>
</Application>
Create a file (e.g. SharedResources.xaml) as follows:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Your.Namespace.Here;assembly=Your.Assembly.Here">
< local:BoolToLightConvertor x:Key="LightConverter" / >
</ResourceDictionary>
In your App.xaml add the following lines:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SharedResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>
You can now use this converter from within the XAML
(The <Style TargetType="{x:Type Rectangle}"/> is a workaround to stop a WPF bug ignoring your resource dictionary and was recommended by another question here on SO. The link unfortunately eludes me now though)
I have the following set of code in my App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>
<ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The NavigationPanel.xaml contains a style that looks like this:
<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
<Setter Property="Background" Value="{StaticResource DarkBackground}" />
<Lots of XAML>
</Style>
The {StaticResource DarkBackground} is defined in the Brushes.xaml file (i.e. the first resource dictionary). It is defined as
<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />
in the resource dictionary.
At runtime, I get the following error:
Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]
The line numbers and position references the NavigationPanel.xaml resource dictionary in the app.xaml.
I can reference the brush from other controls, just not the included resource dictionary.
Why can I not reference or why does it not resolve the reference to a resource that is higher in the heirarchy of the merged resource dictionary?? What am I missing here?
Are you referencing the DarkBackground brush in any of the resources in the NavigationPanel dictionary?
If you are you might need to merge the Brushes resource dictionary into the NavigationPanel dictionary.
So in the NavigationPanel dictionary.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
You can include one dictionary in another (like 'using' in C#) like so:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes.xaml"/>
<ResourceDictionary Source="Fonts.xaml"/>
<ResourceDictionary Source="CoreStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
Is this what you are looking for? The Cosmopolitan / Metro project template has a good example of this...
the trully answer is Eric's answer in this site: https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop.
the Brushes.xaml and the NavigationPanel.xaml is parsed independently and then added to the merged dictionary of Application resources so they don't know anything about each other.