Cannot see FontAwesome Icons on Parent Window - wpf

Summary -- I cannot see FontAwesome icons in the designer when looking at my MainWindow.xaml view that includes a control that has the FontAwesome icons in it.
We are using Visual Studio 2012 to develop a WPF application using Simple MVVM. In our application, we use FontAwesome for most of our icons. I created a resources library that has a reference to FontAwesome (AppStyles.xaml).
Here is an excerpt from my AppStyles.xaml file that references FontAwesome.
<Style x:Key="FontAwesome" TargetType="Control">
<Setter Property="FontFamily" Value="/Fonts/#FontAwesome" />
</Style>
I have created a HeaderView.xaml view that shows a toolbar of options the user can select from. Example of one of the buttons the user can click is below.
<Button Command="{Binding ShowStartScreenAction}">
<Button.Template>
<ControlTemplate>
<StackPanel Orientation="Horizontal" Margin="0">
<Label Content="" Style="{StaticResource FontAwesomeLabel}" />
<Label Content="Home" Style="{StaticResource ButtonLabel}" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
On the HeaderView.xaml view, the FontAwesome icons show up correctly (). A house in this case.
In my MainWindow.xaml view, I have the HeaderView.xaml view added as a control.
<ctrls:HeaderView Grid.Row="0" Grid.ColumnSpan="2" />
The problem I am having is that the FontAwesome does not show up correctly on MainWindow when I view it in the Designer, so I cannot see how the HeaderView.xaml and all my other controls look together inside MainWindow.
Here is my reference in MainWindow.xaml to my resources library where the FontAwesome reference is.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Styles/AppStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
The application runs fine, but it would be nice to be able to see what I have in the designer without having to run the application to see it every time. Can someone help?

Related

Embedding one WPF app into another - Resource file not working

I have a requirement to embed one of our existing applications in our new one. I figured it would be quite easy, so I set made all the content of the origional application inside a UserControl "EmbeddedApp", with the theme applied in it's resources:
<UserControl x:Class="App.GUI.Window.EmbeddedApp"
...>
<UserControl.Resources>
<ResourceDictionary Source="/App;component/GUI/Themes/Theme.xaml" />
</UserControl.Resources>
...all the app stuff...
</UserControl>
And the window for this as a stand alone app then just looked like this:
<Controls:MetroWindow x:Class="App.MainWindow"
...>
<Window:EmbeddedApp/>
</Controls:MetroWindow>
Nice and easy. I tested the "embedding" function in a test application in that project and it works fine.
However, I'm getting resource issues when I embed it into a "full" application like this. There's an exception "Exception: Cannot find resource named 'AccentSelectedColorBrush'. Resource names are case sensitive." on line:
<Rectangle Name ="container" Grid.Row="0" Height="50" Fill="Black" Stroke="{StaticResource AccentSelectedColorBrush}"/>
in the depths of the embedded App. How could this be happening if this is defined in a static resource in Theme.xaml? Why would it work fine in an "empty" application?
I tried adding the theme manually:
Now, Here AccentSelectedColorBrush is found for the rectangle. However, I'm still getting the error AccentSelectedColorBrush not found within EmbeddedApp, it's also White - which isn't as it's defined in Theme.xaml.I can inspect it and the rectangle is resolving it fine:
If I remove the Theme.xaml, AccentSelectedColorBrush is not found.
Edit: I now have this:
<UserControl x:Class="MyNewApp.Pages.Views.EmbeddedAppView"
InheritanceBehavior="SkipAllNow"
...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/App;component/GUI/Themes/Theme.xaml" />
<!--<ResourceDictionary Source="pack://application:,,,/App;component/GUI/Themes/Theme2.xaml" />-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel Height="447" Width="539">
<Rectangle Width="100" Height="100" Fill="{StaticResource AccentSelectedColorBrush}"/>
<!--<App:EmbeddedApp Width="300" Height="300"/>-->
<Label Content="Texty Text"/>
<TextBox Text="Some Text"/>
<CheckBox IsChecked="True" Content="Test"/>
</StackPanel>
</UserControl>
Now, I'd expect the label and TextBox to be styled acording to Theme.xaml, but it's not doing that it's just using the default themes:
Yet in my "test" project, the Theme.xaml is applied fine:
So my conclusion is that:
<ResourceDictionary Source="/App;component/GUI/Themes/Theme.xaml" />
is not correct for an externally referenced project. I tried:
<ResourceDictionary Source="pack://application:,,,/App;component/GUI/Themes/Theme.xaml" />
but that has similar failure. It's definately finding it though, if I misspell and say put "Theme2.xaml" it comes up with an error. But for some reason it's not being applied properly.
Edit2 I've tracked down the problem to how resources are referenced between user controls. I've decided to create a new question that focuses on the actual problem at hand:
"Sub" UserControl cannot find resource - how to get resources to "flow" down the visual tree (without placing it in App resources)

Adding a custom style to an element in an existing theme in WPF

I don't think this has been asked before, if so please redirect me. I'm new to WPF, and I've tried everything I could think of with no success, I'm stuck.
I'm using a WPF Theme, and I want to add some custom styles I created to it. For example, all TextBlock are supposed to have Red foreground, but I have a group of TextBlock that I want in Blue.
So far I've been doing this in the xaml, creating a <Style></Style> in the resources, and calling it using Style="{StaticResource StyleName}". But I want to add it to the theme files instead, and I don't know how to give it a name and call it from the xaml.
I guess I'd start with something like this, but how do I link both elements?
In the theme file (MyStyles.xaml or TextEdit.xaml or similar):
<Style TargetType="{x:Type TextBlock}" x:Key="KeyName" ???>
<Setter Property="Foreground" Value="Blue" />
</Style>
And then in my xaml:
<TextBlock Name="TextBlockName"
Style="{???}">
</TextBlock>
I need this style to be in the Theme because the program will allow users to change themes, and these styles can't hardcoded be in the xaml.
You want to first merge that resource file into your resources :
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<dxc:IntToBooleanConverter x:Key="IntToBooleanConverter" />
(...)
</ResourceDictionary>
</UserControl.Resources>
and then you can use it with
<TextBlock Name="TextBlockName" Style="{StaticResource KeyName}" />
If you have loaded your Theme file you can access any of the Styles/Resources the same way as local Styles/Resources
If you use Style="{StaticResource StyleName}" it will look first in the Window/UserControl, if not found it will look though the loaded Resource dictionaries. so as long as you have loaded the Theme (Resource Dictionary) it will work fine.

Default Styles not resolving from external assembly at runtime

I’m having a bit of trouble with resolving resources from an external assembly.
I have a Styles.xaml file in a project called Theme and I have a Default Button style which has been declared as follows:
<Style TargetType="{x:Type Button}" x:Key="{x:Type Button}">
<!--Setters here-->
</Style>
And then in a separate WPF project (but in the same solution) I have the following in the app.xaml file:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Theme;component/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Then in the main Window, I declare a default Button with no style attached to it like so:
<Button Width="100" Height="100" />
In design view, the button appears to pick up the style from the Styles.xaml file, but when I build and run the project, the Button just defaults to the standard button style.
I have checked to see that the Theme.dll file has been copied across to the WPF build directory (and it has) so I don’t know why this is happening.
Interestingly, if I define the Button Style like this
<Style TargetType="{x:Type Button}" x:Key="MyStyle">
And then reference it directly on the Button in the other project like this
<Button Style={StaticResource MyStyle} Width="100" Height="100" />
It picks up the style in design view and works normally when the project is built and executed.
Any ideas? Any help would be great!
Kris
You may need to use a complete pack URI where you reference the XAML file, namely with siteoforigin if you don't reference embedded resources.

How to determine if a custom control is in a Toolbar?

i have created a UserControl to make an ImageButton:
<Button x:Class="myimagebutton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:imagebutton">
<Grid x:Name="grdButton">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
x:Name="btnImage"
HorizontalAlignment="Left"
VerticalAlignment="Center">
</Image>
<TextBlock Grid.Column="1"
TextWrapping="Wrap"
Text="{Binding Text}"
VerticalAlignment="Center"
Margin="2 0 2 0" />
</Grid>
</Button>
now i want to apply the default Toolbar Button Style to my Button if this Button is in a Toolbar. I have read this article link text and put this
If Me.Style Is Nothing AndAlso TypeOf Me.Parent Is ToolBar Then
Me.Style = DirectCast(FindResource(ToolBar.ButtonStyleKey), Style)
End If
in my Code behind.
After that as a test I put my Button both in a Toolbar and another out of the Toolbar to test it. But the Button always get the default style, not the style I am trying to set.
After debugging i find out that Me.Parent is always Nothing. So now is my question: how i get the information that my button is in a toolbar or not?
I'm having some difficulty understanding exactly what you are describing but after reading it through a few times I think I understand.
Am I right so far?
If so, you are wondering then why your button has an image
A few pointers about your description that threw me off and is probably the reason why you haven't seen anybody else post an answer for your question thus far.
i replaced the the UserControl Item with a Button
Essentially what you have done is created new control that likely inherits from Button. You might have started off with a UserControl but in order to replace the root item in XAML you would also have to make sure your type myimagebutton inherits from Button as well. This is just how XAML works and learning how to explain it this way will help people understand what you are doing.
Normally inheriting from Button is not how developers override the visual style of a button in WPF mainly because WPF doesn't support the concept of what is sometimes referred to as visual inheritance and also there are other suitable methods that can be used to solve the problem in a different way. Instead inheritance is mainly reserved for when behavioral modifications or additions need to be made to an existing control class. This being said there are ways to simulate visual inheritance through the use of content controls that work similar to content pages and master pages in ASP.NET but I think this is a bit outside of the scope of your example. Also if you are to pursue the inheritance model you will need to make sure that in your code behind that you are setting the correct default style in the static constructor so posting your code behind for your button would help too.
I believe the reason why your example isn't working is because the ToolBar specifically looks at the types of controls irrespective inheritance in order to to apply it's custom toolbar styles. In your case your control is of type myimagebutton and not Button so the style is not set by the ToolBar which normally directly sets the Style property based on the type of the control using the two potential types of calls.
element.SetResourceReference(FrameworkElement.StyleProperty, styleKey);
element.DefaultStyleKey = styleKey;
BTW, in your case I believe only the second line is performed by the ToolBar control and styleKey at that point is defined as null.
Now instead of inheriting from Button in the first place you would probably be better off just to create a new ControlTemplate or a DataTemplate for your button and assigning into the Template or ContentTemplate property respectively through the use of a style. This way you are still always dealing with a button and the style is what changes the visual properties.
<Window x:Class="HeaderedContentControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="252"
Width="372">
<Window.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Background"
Value="Orange" />
</Style>
<DataTemplate x:Key="ImageButtonDataTemplate">
<Grid x:Name="grdButton">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
HorizontalAlignment="Left"
VerticalAlignment="Center">
</Image>
<TextBlock Grid.Column="1"
TextWrapping="Wrap"
Text="{Binding}"
VerticalAlignment="Center"
Margin="2 0 2 0"
Background="Pink" />
</Grid>
</DataTemplate>
<Style x:Key="ImageButtonStyle"
TargetType="{x:Type Button}">
<Setter Property="ContentTemplate"
Value="{StaticResource ImageButtonDataTemplate}" />
</Style>
</Window.Resources>
<Grid Margin="11">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ToolBar>
<Button Style="{StaticResource ImageButtonStyle}"
Content="Some Text" />
</ToolBar>
<Button Grid.Row="1"
Style="{StaticResource ImageButtonStyle}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Content="Some Text" />
</Grid>
</Window>
Using the ContentTemplate allows you to redefine the inner contents of the Button without loosing all of the special button state transitions and other niceties you would normally like to keep.
See this related post on MSDN Forums that also explains similar behavior when adding a StackPanel containing buttons to a ToolBar.

WPF - Add Custom Font

I'm trying to add a custom font as a resource in my application.
I have a "CustomFont" directory in the application and all the fonts inside of it are set to "Resource"
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily"
Value="/CustomFonts;Component/#Gotham-XLight" />
</Style>
</Window.Resources>
And then on my TextBlock I have this: (inside a grid)
<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}"
TextAlignment="Center" FontSize="25" FontWeight="Bold"
Foreground="White" Text="TextBlockTimer"
Margin="105,242.974,0,226.975" HorizontalAlignment="Left"
Width="221.919" />
But I'm not seeing my font as people say. Am I doing something wrong?
You may want to check the name of the font, you need to specify the name of the font not the name of the file.
Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.
Try this
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
</Style>
</Window.Resources>
Also, if you are not planning on changing the style at runtime {StaticResource Gotham-XLight} will be much more performant.
In xaml I did it like this:
<Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
<TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome"></TextBlock>
</Button>
However, I don't know if #FontAwesome is font's embedded name or is it the result that I renamed the .ttf file.
Hope to be helpful!
Late reply but worth mentioning. To add a custom font that will apply globally in your window you could add this in your csproj file to include the fonts from the Fonts folder of your project as resources.
<ItemGroup>
<Resource Include="Fonts\*.ttf" />
</ItemGroup>
Then in your window XAML you can specify the FontFamily in the Window part:
<Window x:Class="Namespace.MainWindow"
...
FontFamily="/Fonts/#[FONT NAME]"
Title="">
<Grid>
...
</Grid>
</Window>
I hope this could help somebody, as I spent some time to figure it out.

Resources