Is it possible to set application wide theme from XAML in Telerik? There is only a code-behind example. I would like to set in in XAML and have a Metro theme in design-mode too.
You could achieve this as follows:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SilverlightApplication.App">
<Application.Resources>
<telerik:MetroTheme x:Key="Theme" IsApplicationTheme="True"/>
</Application.Resources>
</Application>
This did not work for me at first. It threw the follwoing exceptions:
{System.Collections.Generic.KeyNotFoundException: The given key was
not present in the dictionary. at
System.Collections.Generic.Dictionary`2.get_Item(TKey key) at
System.Windows.ResourceManagerWrapper.GetResourceForUri(Uri xamlUri,
Type componentType)}
Set property 'Telerik.Windows.Controls.Theme.ApplicationThemeSetter'
threw an exception. [Line: 53 Position: 70]
I tried declaring it in the App.xaml and the Generic.xaml but the same result.
I also tried to set the theme in the XAML like:
<t:RadGridView t:StyleManager.Theme="Windows8Theme" ..../>
But this has no affect.
NOTE: I'm using 2012's Q3
I solved the problem eventually. You have to include the theme DLL. So in my case I wanted to use the Windows8Theme, I needed to add a reference to the Telerik.Windows.Themes.Windows8.dll. It would be helpful if Telerik's code throw an exception saying as much, but "hey", that's just me.
Rashad
Related
When defining WPF styles, I often use the Expression Blend <d:Style.DataContext> tag to let Intellisense know what the runtime DataContext will be. It works great. Unfortunately I cannot seem to make this work in a resource dictionary and I am unclear as to why.
For example, here in a style for a RadTabItem, I tell Intellisense that SettingsPageVm will be the DataContext:
<UserControl x:Class="Views.ConfigureView"
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:tk="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:viewModels="clr-namespace:ViewModels"
d:DataContext="{d:DesignInstance viewModels:ConfigureVm}">
<Grid>
<tk:RadTabControl ItemsSource="{Binding Categories}" >
<tk:RadTabControl.Resources>
<!-- Tell setters that SettingsPageVm will be the datacontext -->
<Style TargetType="{x:Type tk:RadTabItem}">
<d:Style.DataContext>
<x:Type Type="viewModels:SettingsPageVm" />
</d:Style.DataContext>
<!-- (Imagine many setters here, binding to SettingsPageVm) -->
This works fine.
But if try I move that same style to a separate ResourceDictionary Visual Studio complains. The editor puts a blue squiggly underneath the d:Style.DataContext declaration and highlights it red. The compiler complains about it as follows:
1>I:\Dev\MyApp\Resources\Styles\DialogStyles.xaml(13,14): error
MC4004: Style cannot contain child 'TypeExtension'. Style child must
be a Setter because it is added to the Setters collection. Line 13
Position 14.
Why is this valid in one context and not valid in another?
<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"
xmlns:viewModels="clr-namespace:ViewModels"
xmlns:tk="http://schemas.telerik.com/2008/xaml/presentation"
>
<Style x:Key="SettingsPageTabStyle" TargetType="{x:Type tk:RadTabItem}">
<d:Style.DataContext> <!-- *** ERROR *** -->
<x:Type Type="viewModels:SettingsPageVm" />
</d:Style.DataContext>
</Style>
</ResourceDictionary>
Answering my own question because half a day later I stumbled on it.
I had neglected to add the
mc:Ignorable="d"
line to my XAML. Once I did that, everything worked fine.
(Just One of those obscure WPF lessons that I am sure I already learned and then forgot at least once before -- maybe twice -- in the past over several years of working on WPF. )
I'm trying to reference App.xaml's ResourceDictionary from a separate WPF window. I want to use resources from there in different windows, and this seems like the recommended way to do it. Unfortunately, I don't seem to be able to effectively reference App.xaml from the other window. Here is my App.xaml:
<Application x:Class="View.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ViewModel="clr-namespace:ViewModel;assembly=ViewModel"
xmlns:local="clr-namespace:View"
StartupUri="ClockView.xaml">
<Application.Resources>
<ResourceDictionary>
<local:PriorityToIconConverter x:Key="PriorityToIconConverter" />
</ResourceDictionary>
</Application.Resources>
Notes: I'm not using MainWindow, so I've replaced the startup URI with a form that always comes up. I noted that in some other answers, the location of MainWindow is sometimes the issue. In my case, I haven't seen any difference between using ClockView or MainWindow. Both ClockView and MainWindow exist in the root namespace, MainWindow is just never loaded. I also have more resources, but I've removed them for the sake of conciseness.
Here's a simplified example of the code where I'm trying to reference the ResrouceDictionary from App.xaml:
<local:AssistantWindow
x:Class="View.AutomatorView"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:properties="clr-namespace:View.Properties"
xmlns:local="clr-namespace:View"
mc:Ignorable="d"
Title="Tool"
x:Name="Tool"
Background="Transparent"
Height="600"
Width="450"
Topmost="{Binding Source={x:Static properties:Settings.Default}, Path=ToolAlwaysOnTop}"
MinHeight="515"
MinWidth="150">
<Window.Resources>
<ResourceDictionary Source="App.xaml" />
</Window.Resources>
Again, this is simplified to be concise. When I try to load this form, I get the exception:
System.Windows.Markup.XamlParseException: ''Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '21' and line position '10'.'
Inner Exception
IOException: Cannot locate resource 'tool/app.xaml'.
The view for "Tool" is located in a folder that is also named "Tool." However, the xaml and code behind don't reference this namespace, I'm just using the folder to organize my classes. It looks like it's looking for App.xaml in the folder the view resides in. App.xaml resides in the root namespace (View). I've tried modifying the source in the xaml for Tool to:
- View.App.xaml
- View:App.xaml
- View/App.xaml
How can I get this reference to work, so I can share resources throughout my application? Thank you.
You can't load App.xaml like you're trying to do because it's not actually a ResourceDictionary. You can only specify ResourceDictionary files as the target of Source.
However, if you declare a resource in App.xaml, you can reference it anywhere without needing to load the file it's in. That's done for you automatically. Therefore, you can reference your converter at any time with {StaticResource PriorityToIconConverter}.
Note that if you moved it from the default starting location (the base project folder) you may have to update its location. Right click your project, then Properties. Navigate to the "Application" tab (should be the uppermost element on the left-hand sidebar) and look for the "Startup object" field. Set that to [ProjectName].[Namespace?].[Namespace?].App. When I tested it, mine worked without needing to manually change the location, but your setup may be different.
I just got VS2013 and opened my VS2010 project. However now I cannot see any of xaml designs. The problem is the Styles are not being applied:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Gives me the error:
Error 1 An error occurred while finding the resource dictionary "/Themes/Colors.xaml".
This works fine in VS2010. The build option is set to Page. All target frameworks are set to .NET 4.
The directory structure is this:
Host\Themes\Colors.xaml
Plugins\EqPlugin\Source\ProblemFile.xaml
The themes are defined in the VEParameters project and have this declared:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EQPlugin;assembly=VEParameterTool">
The file where the error occurs is in the EqPlugIn directory and has this declared:
<UserControl x:Class="EQPlugin.EQControl"
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:src="clr-namespace:VEParameterTool;assembly=VEParameterTool"
xmlns:l="clr-namespace:EQPlugin"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:oxy="http://oxyplot.codeplex.com"
mc:Ignorable="d">
I have tried changing the paths to relative paths but that doesn't work, i have removed and re-added the assemblies, i have restarted etc, all projects are build for the same framework. I don't know what else to try.
Try the below one, Provied the assembly name, even if it is in the same assembly
Source="pack://application:,,,/<assemblyName>;component/Themes/Colors.xaml"
I have a MVVM WPF application with a simple ListView. The ViewModel currently just contains a List property. I added a DesignData/SampleData.xaml file, set its Build Action to "DesignData" and referenced it in the Window's XAML code
<Window x:Class="..."
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="..."
mc:Ignorable="d"
Title="MainWindow" Height="483" Width="932">
<Grid d:DataContext="{d:DesignData Source=./DesignData/SampleData.xaml}">
But nothing shows up in the designer and VS2010 just gives me the following warning:
The file './DesignData/SampleData.xaml' is not part of the project or its 'Build Action' is not set to 'Resource'.
No matter what the "Build Action" it still gives me the same warning.
SampleData.xaml:
<MainWindowViewModel xmlns="clr-namespace:..."
xmlns:local="clr-namespace:...">
<MainWindowViewModel.Orders>
<local:Order OrderId="1000654321"/>
</MainWindowViewModel.Orders>
</MainWindowViewModel>
Any ideas on how to fix that?
Edit:
Project structure
The DesignData path is wrong as I thought it was relative to the solution/project whereas it seems it is relative to the containing XAML file.
I am converting some Silverlight XAML into WPF. I currently have a user control (MyControl) that is trying to include a couple of other controls that are custom buttons (MyButton1) that are within the same assembly. I have the following XAML that compiles and works in SL:
MyButton1
<UserControl
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"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyButton1"
d:DesignWidth="640" d:DesignHeight="480">
...
</UserControl>
MyControl
<UserControl
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:somename="clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyControl"
d:DesignWidth="640" d:DesignHeight="480">
<somename:MyButton1 />
</UserControl>
When I try to compile this code in WPF, I get the following error:
The tag 'MyButton1' does not exist in XML namespace 'clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName.'
The weird thing is, if I comment out the <somename:MyButton1 /> line of code and compile and then type in <somename: IntelliSense gives me the option to autocomplete MyButton1. Which suggests that the control itself is in the assembly but for some reason it is not being seen when the MyControl XAML is being compiled.
For some context, I took the SL csproj file and made some modifications to it manually to make it a WPF csproj file. If there is a possibility that this caused this funkiness to happen, I'd be glad to share relevant portions of the project file.
Found the answer on an MSDN forum. Turns out that the assembly=MyCompany.MyAssemblyName line in my xmlns was screwing things up. Once I removed that line, I was able to reference the controls.
Related Link: http://social.msdn.microsoft.com/Forums/en/wpf/thread/807c9b80-81c7-4f75-aa2f-8427e17b1a90
To reference the current assembly you must not type its name but leave it blank, i.e. assembly=, the other option is of course to drop it completely. (MSDN -> Mapping to Current Assemblies)