WPF icon repository - c#

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>

Related

Unable to use TextBox after including MaterialDesignInXaml

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"

Apply Aero style on WPF library

I want to force the Aero style on my forms. I created a "WPF Class Library" and added a form and controls to this form. As the library will be called by 3rd party C# applications running on different OS, I want to force to always apply the Aero style.
But as it is a WPF class library I have no App.xaml file where I could put my Resource Dictionary.
I placed it in a dedicated custom Styles.xaml therefore and in my form I reference it like
<Window.Resources>
<ResourceDictionary Source="Styles.xaml">
</ResourceDictionary>
</Window.Resources>
where Styles.xaml looks like
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyLib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;V4.0.0.0;component/themes/Aero.NormalColor.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
and is set to "Build action: Embedded resource" and is stored in the main project folder. I also added a reference to PresentationFramework.Aero in my project.
When I call my library form from the C# application, I get an error saying
Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number 'x' and line position 'y'
What did I do wrong?
This should work for you.
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
Refere this MSDN article for more details.
UPDATE
Include ResourceDictionary in PCL
Create a ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- store here your styles -->
</ResourceDictionary>
You can use it from Your PCL in WPF App
<Window x:Class="Test.Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window"
Height="300"
Width="300">
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/YourResDictionaryFolder/Dictionary1.xaml" />
</Window.Resources>
<Grid>
</Grid>
</Window>
Your.Base.AssemblyName = Dll name
YourResDictionaryFolder = Folder where you created your ResourceDictionary
Dictionary1.xaml = File name which you created above

MahApps and Property Grid

First of all, great thanks to MahApps. What a cool project!
I have an existing application written in WPF that I have applied the MahApps library to. I used this tutorial:
http://mahapps.com/guides/quick-start.html
However the effect on the Property Grid (Xceed) is minimal.
The combo boxes in my other windows look like this:
The property grid combo boxes still look like this (ugly!):
However clicking on a combo box shows the right MahApps style for the items. It is only the Combo Box itself (closed) that is not flat.
My knowledge on WPF is basic. Where would I start to try and fix this? Do I need to manually override the combo box template in the Property Grid?
in MainWindow.xaml use Controls:MetroWindow
<Controls:MetroWindow x:Name="MainApp" x:Class="AppWin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
MinHeight="700"
MinWidth="1024"
>
in MainWindow.xaml.cs inheritance MetroWindow
namespace AppWin
{
public partial class MainWindow : MetroWindow
{
...
add App.xaml following settings
<Application x:Class="AppWin.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AppWin"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
/*--change template color for example green.xaml--*/
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/red.xaml" />
/*--change template style for example BaseDark.xaml--*/
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Perhaps your other combobox looks ugly because the MahApps resources is not found?
Place the mahapp resources you are using in a resource dictionary in the App.xaml file so it will be accessible for all windows. (and not place them in a resource dictionary in only one window, ie. mainwindow.xaml)
App.xaml:
<Application... >
<Application.Resources>
<ResourceDictionary>
<!-- My other resources -->
<!-- ... -->
<!-- MahApps resources -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The property grid overrides the styles of MahApps. You have to create a own PropertyEditor for your properties. The PropertyEditor overrides the styles of the property grid.
I know thats a lot of work, but its the only way to get the MahApps look.

How to include two resources in Page.Resources?

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>

WPF Views not displaying certain controls

I am using Caliburn.Micro and Telerik's controls. I'm new to these so I'm assuming I am making a stupid mistake.
I have the below, VERY simple View. It's just a User Control with a GridView in it. How ever it doesn't show the GridView. Additionally I have a View that is a User Control that just shows a DataForm that also doesn't show.
I get the User control but it's blank. If I throw other controls on there they show, like a RadWatermarkTextBox or a simple TextBlock.
What am I missing? I don't get any exceptions or warnings.
<UserControl x:Name="ModifyAuthUserControl" x:Class="Green.Views.ModifyAuthView"
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:cal="http://www.caliburnproject.org"
MinWidth="500" Width="600" MinHeight="550" Height="600">
<Grid Background="White">
<telerik:RadGridView x:Name="ExistingAuths"/> <!--This doesn't show-->
<telerik:RadWatermarkTextBox Text="HELLO!" /> <!--This DOES-->
</Grid>
The issue was that I had not included ALL necessary references in my App.xaml. Those two controls have multiple dependencies and I only had some of them included. Once I included the bottom two Dictionary entrees everything worked great.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:AppBootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Navigation.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Docking.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Data.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.GridView.xaml"/>
</ResourceDictionary.MergedDictionaries>
Note that your RadGridView and your RadWatermarkTextBox do not have associated grid rows or columns, and will appear one on top of the other.

Categories