I am new in WPF. I want to use Font-awesome Icon in textbox and button. but the icon is not bind with my textbox
I install Font-awesome resource to my application.
Let me know the way how can I use it
Thank You,
I really need it please help me..
Example
Step 1 : Download Font-Awesome
Tools -> Library Package Manager -> Package Manager Console
Install
PM > Install-Package FontAwesome.WPF
Step 2 : Add Resource
<Application> xmlns:fa="http://schemas.fontawesome.io/icons/" </Application>
Step 3 : Put App.xaml
<Application.Resources>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#FontAwesome" />
</Style>
</Application.Resources>
Step 4 : Use it in Demo.xaml
<TextBlock Style="{StaticResource FontAwesome}"
FontSize="75"
Text="" />
Step 5 :- Output
First, download Font Awesome, extract the ZIP file and copy fonts/fontawesome-webfont.ttf into a Fonts folder in your solution. Set the Build Action in the properties to Resource if it isn’t already
Next, add a Style to the Resources in App.xaml. Don’t forget the # at the front of the font name and remember to use the internal name of the font, not the name of the file. To check the name of the font, just double click on the font file and it will open in the Windows Font Viewer. The font name will be at the top.
<Application.resources>
<FontFamily x:Key="FontAwesome">/Fonts/fontawesome-webfont.ttf#FontAwesome</FontFamily>
</Application.resources>
Open MainWindow.xaml and replace the grid with below snippet:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="I" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock x:Name="tbFontAwesome" Text="" FontFamily="{StaticResource FontAwesome}" Foreground="Red" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="Font Awesome" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Grid>
Notice "Text" property of "tbFontAwesome" textblock, its the Unicode for Heart.
Cheat Sheet
To extend the accepted answer because it's somewhat out of date and missing information, here's what I did:
Download FontAwesome
Unzip the archive
Inside the unzipped folder, under the use-on-desktop folder, locate the version you want. N.B. Solid has the most icons free; some icons require a Pro payment for Regular and Light versions.
For me, this was Font Awesome 5 Free-Solid-900.otf.
Following the accepted answer and most tutorials, first create a sub-folder in your C# project named Fonts. Paste the fonts file inside this folder.
I renamed this file FontAwesome.otf for brevity
Set the properties of this file:
Build Action: Resource
Copy to Output Directory: Copy if newer/Copy always
In your App.xaml <Application.Resources> or other <ResourceDictionary>, insert:
<FontFamily x:Key="FontAwesome">/YOUR_PROJECT_NAME;component/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid</FontFamily>
Replace YOUR_PROJECT_NAME with your project name. I include this because it is needed should you use MergedDictionaries across projects.
If you did not place the file in a project sub-folder Fonts, rename or omit this part of the path.
Check that the filename matches: replace FontAwesome.otf with the filename (or rename the file itself).
Check the internal font name. You can do this by following the accepted answer. (Open the .otf or .tff file from explorer to start Windows Font Viewer, copy the font name).
Replace the Font Awesome 5 Free Solid with the font name (after the #).
Do not install the font otherwise you cannot verify that you have followed these steps correctly and the font may not work across computers that do not have the font installed.
I know this is an old question, and this option may not have been available to you at the time, so I thought I would provide an updated method for anyone who stumbles across this. I recommend you install the NuGet package 'FontAwesome5' from here:
FontAwesome5 on NuGet.org
or search "fontawesome5" in Visual Studio's built-in NuGet Package Manager window:
Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
-it is usually the top result when using this exact search term.
Then simply add the following namespace to your XAML document:
xmlns:fa5="http://schemas.fontawesome.com/icons/"
and then you should find relative ease in adding icons to your project. For example:
<fa5:ImageAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
or
<fa5:FontAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
And the beauty of this is that IntelliSense will show you a list of all available icons, saving you the hassle of going to the website to search them up.
You could as well manually select FontAwesome from the FontFamily property of the TextBlock.That will solve the problem.
If FontAwesome is not among the list of fonts then you probably need to import the font file just like the first answer suggested.
Related
I have a RibbonSplitButton that contains a SmallImageSource like so:
<UserControl x:Class="myProgram/toolbars/MainToolbar">
<RibbonSplitButton SmallImageSource="/Images/Undo.png"/>
<!-- more buttons-->
</UserControl>
I try to use this in another file (main):
<UserControl x:Class="myProgram/main"
xmlns:toolbar="clr-namespace:myProgram.toolbars">
<toolbar:MainToolbar/>
</UserControl>
I get the error message in main:
Cannot locate resource 'images/undo.png'
However, when I switch to a normal button:
<Button>
<Image Style="{StaticResource buttonstyle}">
<Image.Source>
<BitmapImage DecodePixelWidth="40" UriSource="/Images/Undo.png"/>
</Image.Source>
</Image>
</Button>
the error is gone. I have tried using Pack URIs, changing the Build Action to Resource and Embedded Resource, as well as Clean Soultion, Rebuild Solution, Restarting Visual Studios (rinse and repeat). I image this has something to do with the RibbonSplitButton's image source, but I have no idea. The program runs fine (the Undo.png image shows and works perfectly), it's just annoying to have the error in my developer. Anyone have suggests on how to get rid of the error? (Note, this is a simplified folder structure, for example purposes).
EDIT
For anyone who ends up here at some point in the future, I found that this answer worked.
I have file 'resource.xaml' for all the styles. But I need to add a new font file to my project and re-use it instead of default font. How can I add a new font and use it?
I searched the internet and see some methods. but nothing worked for me.
Thanks in Advance.
when using a custom font you must respect the following syntax:
"/FontPath/FontFileName.ttf#FontName"
for exemple :
<Setter Property="FontFamily" Value="/Fonts/VLADIMIR.TTF#Vladimir Script"/>
where the name of this font is "Vladimir Script".
I've had this problem before, and here's how I did it:
Create a folder in your solution and add all of the font .ttf files in there.
In App.xaml, or a resource dictionary, add the following code:
pack://application:,,,/Path/To/Font/#Name Of Font
Then, you can reference the font resource in your XAML code:
<TextBlock FontFamily="{DynamicResource NameOfResource}" Text="Hello World"/>
I am working on a UserControl in a Silverlight project in visual studio 2010.
This user control uses Labels as follows:
<sdk:Label .../>
-or-
<sdk:Label>
singleObject
</sdk:Label>
-or-
<sdk:Label>stringContent</sdk:Label>
as described here
However, when I create a WPF project link it to Silverlight Library Project:
(by adding existing items (Add Link) to cs classes and the .xaml file defining the control)
I get the following error in build:
The tag 'Label' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'. Line 49 Position 18. myfile.xaml 49 18 MyLibraryWPF
This doc shows that labels in WPF are declared without the sdk:
like this:
<Label>
Content
</Label>
yet, I cannot change that in my Silverlight project. It will be a real waste to create a whole new project and duplicate the code to make it work.
How to make the Labels work with both Silverlight and WPF projects?
Here is a piece of Code in xaml that can be shared by WPF and Silverlight and looks like label with Background coloe:
<Border Margin="3,0,3,3" Background="#FF00B050">
<TextBlock Text="txt122" FontSize="13" FontWeight="SemiBold" Height="21" Name="label15" Width="133" ></TextBlock>
</Border>
This means that a Label cannot be used in a shared XAML.
I have a button on a custom control and I'm trying to display an image on it which is defined as a resource in my App.xaml file like so:
<Application.Resources>
<BitmapImage x:Key="PlusSymbol" UriSource="Resources/PlusSymbol.png" />
</Application.Resources>
For some reason I can't use this as a static resource within my custom control's template defined in the Themes\Generic.xaml file, it crashes my application during runtime saying that it cannot find the requested resource. Here's the xaml code I'm using to bind to the resource:
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
It DOES work during runtime if I define the resource the exact same way but within the Generic.xaml file, however it gives me a pre-compiler warning that it can't find the file since it's now looking for it in Themes/Resources/ rather than just in /Resources/. So I get a warning during design time but it works fine in runtime.
So why can't it find my resource when it's defined in App.xaml? I do this the exact same way in a regular WPF project and it works flawlessly, but in this custom control library it is giving me headaches. Any help is much appreciated!
This should work if you switch your StaticResource to DynamicResource so that the resource will be evaluated dynamically at runtime rather than statically. If you switch to DynamicResource.
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{DynamicResource PlusSymbol}"/>
</Button>
I believe this is because of how theme-based styles and templates are handled, as opposed to standard resources. This answer and this answer speak specifically to Generic.xaml and how it is different from other ResourceDictionaries.
So the way I ended up getting this to work was by defining the BitmapImage in the Generic.xaml file and using a Pack URI to get to the file (here's the MSDN article about Pack URIs, which frankly just confused me). This way it's using a relative path to the file, and specifying the assembly that it's coming from (the file is located at \Resources\PlusSymbol.png in the MyCustomControlLibrary project and has a build action of Resource):
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/MyCustomControlLibrary;component/Resources/PlusSymbol.png" />
Then in the control template in Generic.xaml I use the resource like so:
<Button Grid.Row="0" Grid.Column="1" Margin="3" Height="25" Width="25"
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
Note that I got fooled thinking that I could use the shorter version of the Pack URI like so:
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/Resources/PlusSymbol.png" />
However this was still causing the program to crash at runtime. I think the fact that this is a custom control library, and that the end consumer of the image is my UI project, makes the longer version which specifies the assembly that actually contains the image necessary.
Hope that helps anyone else who is having similar problems.
Note I don't think this technically answers my original question which specified using app-level resources (aka defined in the custom control library's App.xaml file) from Generic.xaml. Defining the PlusSymbol resource there still crashes the program. However I didn't actually care about doing it in App.xaml, I was just trying to get it to work right at both design time and run time. This solution does the trick for me, is fairly simple, and from what I can tell from my research is the best practice.
I have several image files I want to share between projects(common icons) I have them in an assembly that would be in every solution I create...I have the files in a folder called Icon and I have the build as content copy always. I have verified that a folder is created with these icons...however my other assemblies are not able to find them...
<r:RibbonGroup Header="Users">
<r:RibbonButton >
<r:RibbonButton.LargeImageSource>
<BitmapImage UriSource="..\Icons\UserIcon.png" />
</r:RibbonButton.LargeImageSource>
</r:RibbonButton>
</r:RibbonGroup>
i have tried formatting the uri several ways...but it never succeeds. If the icons are in the actual assembly though they work...
Try using an absolute Uri. Build action must be set to Resource for UserIcon.png
<BitmapImage UriSource="pack://application:,,,/NameOfImageAssembly;component/Icons/UserIcon.png"/>
Relative Uri should also work
<BitmapImage UriSource="/NameOfImageAssembly;component/Icons/UserIcon.png"/>