XAML styles in Silverlight not being recognized - silverlight

I am attempting to create a Syles.xaml file for my test Silverlight app. Here is what I have in the App.xaml file:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
x:Class="MVCSilverlight.App"
>
<Application.Resources>
<Style x:Key="NavigationContainerStyle" TargetType="StackPanel">
<Setter Property="Background" Value="Black" />
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="500" />
</Style>
</Application.Resources>
</Application>
The problem is that when I include this in the app, VS2010 does not recognize it AND when I run the application, it does not display because there are errors with attempting to find that resource name/value. Here is an example of how it is being used:
<StackPanel Style="{StaticResource NavigationContainerStyle}">
</StackPanel>
I also attempting to put the styles in a file and include it in the app.xaml but that didnt work either.
Can someone give me some ideas as to why this is happening?

That XAML looks like it should work fine as long as App is still set as the startup object in the project settings and InitializeComponent() is still being called in App.xaml.cs.
If you are putting styles in a Styles.xaml file you will need use a merged resource dictionary to merge it into either the App resources or directly into the resources of the UserControl where you are going to use it.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Related

How to display external fonts in WPF xaml designer?

I am able to display external fonts during run time, but not in the designer.
Most of the topics I came across are for the opposite scenario where it displays in designer, and not during run time.
How do I display external fonts in both design and run time?
Below are my current code.
Resource Dictionary (Dictionary.xaml)
<FontFamily x:Key="Montserrat">pack://application:,,,/xxx;Component/Fonts/#Montserrat</FontFamily>
<Style x:Key="TBRoman" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource Montserrat}" />
</Style>
User Controls
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/xxx;Component/Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Controls
<TextBlock Text="message" Style="{StaticResource TBRoman}" />

xaml ResourceDictionary inside User Control Library

how is it possible to define a ResourceDictionary inside a User Control Library and access them via Xaml-Code.
I've created something like this:
<ResourceDictionary 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"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
>
<Style x:Key="NormalStyle" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontStyle" Value="Normal" />
</Style>
.
.
.
</ResourceDictionary
And now I want to use this "NormalStyle" with a Control
Style="{StaticResource NormalStyle}"
But Visual Studio says "The resource "NormalStyle" could not be resolved."
Did i miss or forget something?
Thanks for helping me
You will have to include or merge your ResourceDictionary with UserControl.Resources like below. Here in Source give path to your ResourceDictionary.
<UserControl.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>
OR
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Then you can use the resources in the dictionary inside your UserControl
Style="{StaticResource NormalStyle}"
Please add style reference in App.xamal therfore so that it loads when you run the application.
When you add any external resource file you need to add it in App.xamal to get it's reference in your app else you need to manually add reference to every xamal form to access style and templates
This is how you going to add it in App.xamal using MergedDictionaries than where ever you refer any style in you application it will have it's reference
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/CommonStyles.xaml"/>
<ResourceDictionary Source="Project/Common/CommonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
Hope that helps
Alright, so I got it half to work. Inside my UserControlLibrary i made a new Xaml-File "UIStandards", where put all my Styles into. Now in my Main Project, i want to have access to these Styles and I think i should use a MergedDictionary.
What i did was this:
<Application x:Class="MentorPlusClientWindowsWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MentorPlusClientWindowsWPF"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Startup="Application_Startup">
<!--StartupUri="MainWindow.xaml"-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CrossProjectUserControls;component/UIStandards.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
As you can see, i referenced the UISTandards.xaml but inside a UserControl inside my MainProject, none of my Styles can be found.
Any solutions?

Trying to change window background color from XAML file

I'm playing around with the ExpressionDark.xaml theme. I'm setting the theme in App.xaml:
<Application x:Class="WpfApplication4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="Themes/ExpressionDark.xaml"/>
</Application.Resources>
</Application>
I looked at how they were setting the colors and styles of other controls, but am unable to produce the same result with the window.
If you like, you can see the XAML here.
Here's the XAML I'm trying:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource BlackTestBrush}" />
<Style.Triggers>
</Style.Triggers>
</Style>
<SolidColorBrush x:Key="BlackTestBrush" Color="#FF000000" />
Any ideas on what I'm doing wrong here?
Thanks

Styles defined in Application.Resources not applying to controls

I am trying to apply global application styles to certain control types, however adding these styles to Application.Resources does is not applying the styles to the elements in my views.
Example:
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="AliceBlue"></Setter>
<Setter Property="Margin" Value="20,20,20,20"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
</Application.Resources>
</Application>
In all the examples I have found for applying application wide styles this has been how they say to do it, however it is not working for me. What am I doing wrong?
Thanks,
Alex.
Worked this out myself woops, the problem is I was not using the StartUpUri property to open my initial application view, I changed my start up process so it does use this property and this has fixed my problem.
My App.xaml now looks like this:
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="/Views/Application/SplashView.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Aqua"></Setter>
</Style>
</Application.Resources>
</Application>
Thanks,
Alex.
Eventhough this is an old post and has been answered. I came across this problem. I removed StartupUri and added and empty style (I used the question as an example):
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<!-- Added blank style first -->
<Style TargetType="Rectangle" />
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="AliceBlue"></Setter>
<Setter Property="Margin" Value="20,20,20,20"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
</Application.Resources>
</Application>

Mixing general WPF styles with ResourceDictionary

I came from web development and WinForms to WPF and maybe I didn't get the concept yet.
I'm able to define general styles for my Application in the app.xaml. For example I defined the style for all my ribbon controls in this file.
Then I tried Microsoft Blend and came across ResourceDictionary, which is somekind of Resource File .resx I knew from WinForms.
But as I see it's not possible to mix these two concepts. For example following xaml code will not work because ResourceDictionary have to be the only child.
<Application x:Class="Wpf.MyApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
StartupUri="MyMainWindow.xaml">
<Application.Resources>
<!-- Resources scoped at the Application level should be defined here. -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/RibbonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<BitmapImage x:Key="IconDokumentNeu" >Images/NewDocument_32x32.png</BitmapImage>
<SolidColorBrush x:Key="LightGrayBrushKey">WhiteSmoke</SolidColorBrush>
</ResourceDictionary>
<Style TargetType="{x:Type ribbon:RibbonWindow}">
<Setter Property="Icon" Value="../time2_32.png" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
</Style>
</Application.Resources>
</Application>
It seems I didn't really get the concept. Maybe you can help me, why this is not possible and how I can use general styles next to ResourceDictionary.
You already have resources defined "next to" the dictionary, one image and one brush.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Dictionaries from file here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other resources here -->
</ResourceDictionary>
</Application.Resources>
Just include the {x:type} style in the resource dictionary
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Dictionaries from file here -->
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ribbon:RibbonWindow}">
<Setter Property="Icon" Value="../time2_32.png" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
</Style>
</ResourceDictionary>

Resources