I'm trying to bind controlTemplate to in generic.xaml.My controlTemplate has converter in it.While binding,its throwing an exception as Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
In MyView.cs
templateString = #"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<Canvas><Polygon Points=""{Binding Size,Converter={StaticResource SizeConverter}}"" Fill=""red""/></Canvas>
</ControlTemplate>";
this.Template = XamlReader.Load(new System.Xml.XmlTextReader(new StringReader(templateString ))) as ControlTemplate;
In generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:MySample"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework"
xmlns:srcview="clr-namespace:MySample.Views"
xmlns:converters="clr-namespace:MySample.Converters"
>
<converters:SizeConverter x:Key="SizeConverter" />
<Style TargetType="{x:Type srcview:MyView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type srcview:MyView}">
<Canvas>
<ContentControl Template="{TemplateBinding Template}" Name="contentControl" >
</ContentControl>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
How to fix this issue??
One normally gets this problem when the static resources are not defined before referencing them.
For a test, try reading an xaml usercontrol with a static resource defined in the resources section of the xaml. If that works then you know the problem is not with referencing the converter, but rather when and were it is defined.
A faster way to ensure that the converter is actually being loaded is to put it in the app.xaml. This will ensure that the resource dictionary is loaded at startup. Here follows an example:
<Application x:Class="TeslaFrame.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:metro="http://schemas.codeplex.com/elysium"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Elysium;component/Themes/Generic.xaml"/>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="globalBoolToVisConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I have a WPF Window:
<Window x:Class="MyUI.MainWindow"
and so on>
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
</ResourceDictionary>
</Window.Resources>
</Window>
I have a resource dictionary in MyResourceDictionary.xaml:
<ResourceDictionary xmlns="........."
and so on >
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle" BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>
But when I try to reference ChildStyle from MyUI.Window:
<Window as shown in 1st block of code above>
<s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</Window>
It tells me that it can't find FatherStyle. I read here and merged the dictionary in MyResourceDictionary.xaml:
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style ChildStyle as shown above />
</ResourceDictionary>
Now it tells me it can't find ChildStyle. How do I reference this properly?
You can't reference a resource dictionary contained in a Window-type XAML file, from another file. What you need to do is create a separate resource dictionary, "Shared.xaml" or whatever:
<ResourceDictionary ... >
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
</ResourceDictionary>
Now reference the shared one from your main window:
... and also from "MyResourceDictionary.xaml":
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Shared.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle" BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>
And now in your "MyUI.xaml" window, you should be able to access the "ChildStyle" as you expected, by referencing "MyResourceDictionary":
<ResourceDictionary xmlns="........."
and so on >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</ResourceDictionary>
How do I access a ResourceDictionary from different project in a solution?
My Solution file contains a few projects. One of these projects is named "Common" and contains a Resource called SharedResource.xaml.
SharedResource.xaml in Common:
<ResourceDictionary 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"
mc:Ignorable="d">
<ImageBrush x:Key="UserIcon" ImageSource="/Images\icon_user.png"/>
</ResourceDictionary>
This is how I tried to make the SharedResource Dictionary usable in one of my UserControls.
<UserControl x:Class="MasterDataManagement.View.DisplayMasterDataView"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
xmlns:common="clr-namespace:Common;assembly=Common"
mc:Ignorable="d"
d:DesignWidth="800" d:DesignHeight="600" d:DataContext="{Binding DisplayMasterDataViewModel, Source={StaticResource ViewModelLocator}}">
<UserControl.Resources>
<ResourceDictionary> <!-- ERROR Each dictionary entry must have an associated key.-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/Resources/SharedResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<!-- Removed Content -->
</Grid>
</UserControl>
Unfortunately, this approach doesn't work and I'm not sure what I missed. The Code above causes this error message: Each dictionary entry must have an associated key.
Any thoughts?
Notes:
I can't put the ResourceDictionary into App.xaml because I create direct UserControl instances(something like UserControl uc = new DisplayMasterDataView() ).
Only the App.xaml from the Project which is set as StartUp Project is "executed". My solution consists out of one MainProject (which is set as StartUp Project) and a few other projects which are used as modules in the MainProject. Now, I need to use this modules in my MainProject which is done like this:
UserControl uc = new DisplayMasterDataView();
My point is that creating an instance of an UserControl from a different project does not execute the App.xaml in the project which consist the UserControl. Therefore I have to use <UserControl.Resources> </UserControl.Resources> to define my resources.
I just mentioned this because the most sample projects use the App.xaml to define Resources.
Each dictionary entry must have an associated key...
Surely that error is just telling you that you need to add a key to your ResourceDictionary using the x:Key notation... in this case, the 'dictionary entry' is your ResourceDictionary and the 'associated key' is whatever you call it to reference it later, in my example below, 'CommonResourceDictionary':
<ResourceDictionary x:Key="CommonResourceDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Common;component/Resources/SharedResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
My WinView project contains a file called ViewResources.xaml in which I place all the resources needed by the controls in that project. It also links in other xaml files in the same project:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainTabControlStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- ==================== Data Templates ==================== -->
<DataTemplate DataType="{x:Type myNameSpace:MyViewModel}">
<controls:MyUserControl MyProperty="{Binding ...etc...}" />
</DataTemplate>
</ResourceDictionary>
My main project then references this in App.xaml:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Main Theme -->
<ResourceDictionary Source="pack://application:,,,/WPF.Themes;component/BureauBlue/Theme.xaml" />
<!-- Global Resources for Views -->
<ResourceDictionary Source="pack://application:,,,/MyProject.WinView;component/ViewResources.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyProject.WinUI;component/HelpResources.xaml" />
... and so on and so forth ...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I don't see how this would stop you from creating UserControls directly.
Here comes the solution, thanks you for pointing me into the right direction Rohit Vats!
Not Working
<UserControl.Resources>
<gridView:RadContextMenuXamlHolder x:Key="ContextMenuHolder">
<telerik:RadContextMenu DataContext="{Binding Path=UIElement.Column.DataControl.DataContext, RelativeSource={RelativeSource Self}}" InheritDataContext="False"
StaysOpen="True" x:Name="CellCellContextMenu" Placement="Bottom" Loaded="ContextMenu_OnLoaded">
<telerik:RadMenuItem x:Name="ColumnsMenuEntry" Header="Choose Columns:" ItemsSource="{Binding Path=Menu.UIElement.Column.DataControl.Columns, RelativeSource={RelativeSource Self}}" StaysOpenOnClick="True">
<telerik:RadMenuItem.ItemContainerStyle>
<Style TargetType="telerik:RadMenuItem">
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="IsChecked" Value="{Binding IsVisible, Mode=TwoWay}" />
<Setter Property="IsCheckable" Value="True" />
</Style>
</telerik:RadMenuItem.ItemContainerStyle>
</telerik:RadMenuItem>
<telerik:RadMenuItem Header="Show all columns" Click="RadMenuItem_ShowAllColumns_OnClick"/>
<telerik:RadMenuItem Header="Hide all columns" Click="RadMenuItem_HideAllColumns_OnClick"/>
</telerik:RadContextMenu>
</gridView:RadContextMenuXamlHolder>
<Style TargetType="telerik:GridViewHeaderCell">
<Setter Property="telerik:RadContextMenu.ContextMenu" Value="{Binding Path=CellContextMenu, Source={StaticResource ContextMenuHolder}}"/>
</Style>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/SharedResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Working
<UserControl.Resources>
<ResourceDictionary>
<gridView:RadContextMenuXamlHolder x:Key="ContextMenuHolder">
<telerik:RadContextMenu DataContext="{Binding Path=UIElement.Column.DataControl.DataContext, RelativeSource={RelativeSource Self}}" InheritDataContext="False"
StaysOpen="True" x:Name="CellCellContextMenu" Placement="Bottom" Loaded="ContextMenu_OnLoaded">
<telerik:RadMenuItem x:Name="ColumnsMenuEntry" Header="Choose Columns:" ItemsSource="{Binding Path=Menu.UIElement.Column.DataControl.Columns, RelativeSource={RelativeSource Self}}" StaysOpenOnClick="True">
<telerik:RadMenuItem.ItemContainerStyle>
<Style TargetType="telerik:RadMenuItem">
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="IsChecked" Value="{Binding IsVisible, Mode=TwoWay}" />
<Setter Property="IsCheckable" Value="True" />
</Style>
</telerik:RadMenuItem.ItemContainerStyle>
</telerik:RadMenuItem>
<telerik:RadMenuItem Header="Show all columns" Click="RadMenuItem_ShowAllColumns_OnClick"/>
<telerik:RadMenuItem Header="Hide all columns" Click="RadMenuItem_HideAllColumns_OnClick"/>
</telerik:RadContextMenu>
</gridView:RadContextMenuXamlHolder>
<Style TargetType="telerik:GridViewHeaderCell">
<Setter Property="telerik:RadContextMenu.ContextMenu" Value="{Binding Path=CellContextMenu, Source={StaticResource ContextMenuHolder}}"/>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/SharedResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
the trick was to move all resources into the <ResourceDictionary> </ResourceDictionary> tag.
I have a custom Color.xaml as
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="MyColor1" Color="#7d897d"/>
<SolidColorBrush x:Key="MyColor2" Color="#078ab4"/>
</ResourceDictionary>
And App.xaml as
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Defines the colors used in the app-->
<ResourceDictionary Source="Color.xaml"/>
<!-- Styles that define common aspects of the platform look and
feel Required by Visual Studio project and item templates-->
<ResourceDictionary Source="StandardStyles.xaml"/>
<ResourceDictionary>
............
In My StandardStyle.xaml I'm not able to use the Color defined in the xaml.
<Style x:Key="HeadingTextStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="SemiLight"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="{StaticResource MyColor1}"/>
</Style>
It gives me an exception when I run the code
"Cannot find a Resource with the Name/Key MyColor1 [Line: 20 Position: 44]"
However I'm able to use this color in the UI xaml files.
Here it is, include the Color.xaml in StandardStyle.xaml as
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Color.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- your styles here -->
</ResourceDictionary>
I need to create resource with name OkButtonStyle based on RedButtonStyle.
But i have resource with key not found exception. What i am doing wrong?
I have two resouce dictionaries. One baseStyles.xaml where RedButtonStyle located and styles.xaml where i need to locate my okbuttonstyle but all my efforts results nothing.
App.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="baseStyles.xaml" />
<ResourceDictionary x:Name="currentTheme" Source="styles.xaml" />
</ResourceDictionary.MergedDictionaries>
styles.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="baseStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="OkButtonStyle"
BasedOn="RedButtonStyle"
TargetType="Button">
</Style>
baseStyles.xaml
<Style x:Key="RedButtonStyle"
TargetType="Button">
...properties...
</Style>
Instead of using the syntax:
<Style BasedOn="RedButtonStyle" ...
use the syntax:
<Style BasedOn="{StaticResource RedButtonStyle}" ...
The Style.BasedOn property is not the name of the other style it is based on, it is the style it is based on.