Why can't I get the custom font from the resources? - c#

I have a library where contains a ThemeResources file, where contain styles.
In Fonts folder, I have a font file BuxtonSketch.ttf. Build Action is as RESOURCE
In ThemeResources, I've defined the font:
<FontFamily x:Key="FontFamily-Sketch">pack://application:,,,/Resources/Fonts/#Buxton Sketch</FontFamily>
<Style x:Key="TextNormalStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="{StaticResource FontFamily-Sketch}" />
</Style>
And since my UserControl called ProblemUserControl, I invoked but is not working
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/ThemeResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<TextBlock Text="La pelicula PROMETHEUS!" Style="{StaticResource TextNormalStyle}" />
</Grid>
What could be happening?

This blog and this blog both use the following syntax to use a custom font
Blog 1:
<TextBlock Text="SOTC Custom Fonts!" FontFamily="VINERITC.TTF#Viner Hand ITC"/>
blog2:
<TextBlock Text="Gothic" FontFamily="./GOTHIC.TTF#Century Gothic"/>
So for you I would think it would be
<TextBlock Text="Buxton" FontFamily="BuxtonSketch.TTF#Buxton Sketch"/>
Or for your style
<Style x:Key="TextNormalStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="BuxtonSketch.TTF#Buxton Sketch" />
</Style>
You may need to include the folder location, but not sure
<TextBlock Text="Buxton" FontFamily="Resources/Fonts/BuxtonSketch.TTF#Buxton Sketch"/>

Also you can add the fonts to .zip file and access like this:
<Setter Property="FontFamily" Value="../Resources/Fonts/Fonts.zip#BuxtonSketch"/>

I just spent a whole day researching this problem. When copying the fonts into the project, make sure Visual Studio set the Build Action of these items to Resource. All I had to do was change this in the dropdown.

Related

How to separate xaml markup into different file in uwp?

I have a very big xaml markup in MainPage.xaml. More I add more it becomes a mess. Can I split my xaml markup into different files or store it in a ResourceDictionary and load it into MainPage.xaml?
Not known to me. What I do is, for example, deposit the sytling of my controls in the App.xaml. For example, for a TextBlock element:
<Application.Resources>
<Style x:Key="TextBlockStyle1" TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="TextTrimming" Value="None"/>
<Setter Property="FontFamily" Value="CalibriLight"/>
<Setter Property="Foreground" Value="WhiteSmoke"/>
</Style>
</Application.Resources>
In Mainpage.xaml I grab it so:
<TextBlock Style="{DynamicResource TextBlockStyle1}"/>
Thus, you save a lot of time while styling and your Mainpage.xaml is much tidier.
Yes,you can create a ResourceDictionary and put your style in it.Then use MergedDictionaries to load it into MainPage.For more information you can view this document.
in ResourceDirectionary(e.g. named Styles.xaml):
<ResourceDictionary.....>
<Style x:Key="ButtonStyle1" TargetType="Button">
...
</Style>
</ResourceDictionary>
in MainPage.xaml:
<Page.Resources>
<ResourceDictionary>​
<ResourceDictionary.MergedDictionaries>​
<ResourceDictionary Source="Styles.xaml"/>​
</ResourceDictionary.MergedDictionaries>​
​
</ResourceDictionary>​
</Page.Resources>
......
<Button Style="{StaticResource ButtonStyle1}">

C# WPF Button Styles inside DLL

I'm making a DLL for me, to ease my job, because there are classes that I use in every project, so why should i duplicate them, when I can use one DLL to finish the job,
I also wanted to add some controls to it, buttons, so its like this:
I have created a button, and it works well, but I want to add a custom style to it, to disable the background highlighting when you are mouse over, now i have used this style before and works well, but in previous times, I would add the style to the app.xaml resources and then set the style to the button like:
Style="{StaticResource DisableBackgroundHighlight}"
but since the DLL does not have app.xaml, what should I do, how to add style to the control inside the DLL?
All I've found on google was, to reference the resources from the DLL to the app.xaml of the WPF app, but thats not what I want,
I tried this:
<Button x:Class="SRX.Windows.Controls.SRXButton"
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="clr-namespace:SRX.Windows.Controls"
mc:Ignorable="d"
d:DesignHeight="35" d:DesignWidth="100" Content="OK" Background="White" BorderBrush="Blue" Foreground="Blue" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Style="{StaticResource DisableBackgroundHighlight}">
<Button.Resources>
<Style x:Key="DisableBackgroundHighlight" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.8" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
but it doesnt work, it shows "The resource "DisableBackgroundHighlight" could not be resolved." altough it compiles but crashes on startup.
If I missed something in the problem explanation please ask me to resolve, thanks in advance.
Simply add a xaml file to your project. Let's call it Generic.xaml which is where usually the templates for your custom coltrols will be located .
This file will have the following format:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Your.Domain.Generic">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="whatever else you defined in another xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox" ....
</ResourceDictionary>
on the other assemblies, you import your "style" assembly just like anything else:
xmlns:style="clr-namespace:Your.Domain.Shared"
Supposing of course that you style assembly is named Your.Domain.Shared

Override dictionary style with local style

I have a user control (a modernTab, provded by modernui) that has a style applied to it, as is specified in a resource dictionary (that again came with modernui).
That's fine, styling for this app is provided through some default resources in the App.xaml file that look like this:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
</ResourceDictionary.MergedDictionaries>
That's well and good. However, I want to override the link style I am using for a specific instance of a modernTab. So in my XAML, I'm trying to do it like this:
<mui:ModernTab ListWidth="Auto"
Layout="List"
Links ="{Binding MyViewModelLinks}">
<mui:ModernTab.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="Yellow" />
</Style>
</mui:ModernTab.Resources>
</mui:ModernTab>
Now, I know from looking at the source that down inside the guts of a modernTab control it's got a bunch of ListBoxItems - these are what I want to change the style on.
What I don't get is why my "local" style isn't going down and overriding for this specific instance. Any ideas?
I tried defining my style override in App.xaml (even though I don't really want it to be global) and it didn't work. Clearly I'm missing something.
What you are doing here is not overriding default style of ModernTab but specifying resources of a particular instance, the style is still taken from ModernTab.xaml
What you need to do here is to specify inline style for your instance of ModernTab:
<mui:ModernTab ...>
<mui:ModernTab.Style>
<Style TargetType="mui:ModernTab">
<!------- Full ModernTab Style ----->
</Style>
</mui:ModernTab.Style>
This inline style will override the default. The bad news is that you cannot create a style based on default ModernTab style and just tweak small details because the default style does not have a name (x:Key). But you can copy the whole style, change whatever you want in it, and use it instead. You should probably put it in a resource file and then use it on your ModernTab instance like this:
<mui:ModernTab Style={StaticResource MyAwesomeStyle} .../>
Hope this helps
You need to "override" the ItemContainerStyle of the ListBox in the ModernTab. This should do the trick:
<mui:ModernTab ListWidth="Auto"
Layout="List"
Links ="{Binding MyViewModelLinks}">
<mui:ModernTab.Resources>
<Style TargetType="ListBox">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="Yellow" />
</Style>
</Setter.Value>
</Setter>
</Style>
</mui:ModernTab.Resources>
</mui:ModernTab>

How to style a WPF AutoCompleteBox?

Probably a really simple mistake, I have downloaded the WPF toolkit and placed an AutoCompleteBox on my window. How can I create a style for this control? I have tried to follow msdn but no luck so far...
http://msdn.microsoft.com/en-us/library/dd728668(v=vs.95).aspx
On my window I have:
<Controls:AutoCompleteBox style="{StaticResource acInput}"/>
In my styles I have the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:input="clr-namespace:System.Windows.Controls.Input"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Style x:Key="acInput" TargetType="input:AutoCompleteBox">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
</ResourceDictionary>
But I have the error:
The name "AutoCompleteBox" does not exist in the namespace "clrnamespace:System.Windows.Controls.Input".
Add the following namespace reference:
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Solved it:
Simply changed TargetType to "Controls:AutoCompleteBox"
<Style x:Key="acInput" TargetType="Controls:AutoCompleteBox">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>

WPF Class Library with custom control - control won't show

After a couple of hours searching the web, I turn to you all:
My WPF class library (.NET 3.5, COM visible) has a form in it, which uses an UserControl and a theme file.
The problem:
When using WPF Applications, the buttons work just fine, but in this .NET 3.5, COM Visible, class library, they won't show at all. Other objects from the library can be used and work.
What could be the problem? I'm leaning towards the resource dictionary that can't be found, or some of the resources that cannot be found, but I can't put my finger on it.
Any help would be most welcome!
Some code
The resources are set via the
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyLibrary;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
There we place - for now - a simple:
<Grid>
<lib:ImageButton ImageSource="/MyLibrary;component/Images/some_image.png" />
</Grid>
Not forgetting the reference for the library in the project and the xamls:
xmlns:lib="clr-namespace:MyLibrary;assembly=MyLibrary"
The MyLibrary holds this ImageButton - a simple button extension mainly to hold an image.
The WPF looks somewhat like:
<UserControl x:Class="MyLibrary.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lib="clr-namespace:MyLibrary"
x:Name="me"
Width="auto"
Height="22"
HorizontalAlignment="Center" VerticalAlignment="Center">
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type library:ImageButton}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
<Setter Property="Width" Value="22" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
<Setter Property="Width" Value="auto" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Button x:Name="_button" Click="Button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="auto">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=me, Path=ImageSource}" Stretch="Uniform" />
<TextBlock x:Name="_Text" Text="{Binding ElementName=me, Path=Text}" VerticalAlignment="Center" Margin="2, 0, 0, 0">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Resources>
</TextBlock>
</StackPanel>
</Button>
</UserControl>
The .cs side is fairly arbitrary I guess.
Finally, we have Generic.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyLibrary;component/Themes/MyTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Where "MyTheme" holds lots of templates, colours etc..
The problem most likely stems from the fact that your Class Library project has no idea that there are resource dictionaries you want to consume everywhere even though you merged your resource dictionaries in some file. The reason for this is that Visual Studio and Blend both look for an ApplicationDefinition file for global resource dictionaries to consume.
Class Library projects cannot have ApplicationDefinition files. However, there is a way you can share resources for design time viewing of your custom control project. A question on that topic and the answer can be found here (disclaimer: the link currently points to my answer).
Please take note that if you use the solution linked to above, you will still have to reference your resource dictionaries at the App.xaml level of any Application project that consumes your class library as your class library will not contain the specific styling..
Well this question is rather old but here goes:
After doing a lot of comparison i verified that the way to get it working is setting the following lines somewhere in your class library on the assembly level:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly
)]

Categories

Resources