wpf silverlight multi-targeting ResourceDictionary - c#

I'm writing some multi-targeting application (.net 4.0). I have problems with ResourceDictionay.
Scenario:
Create WPF CustomControlLibrary - name it "WpfLib", default Namespace "test"
In WpfLib create UserControl - name it "uc"
<UserControl x:Class="test.uc"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="res.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Button Style="{StaticResource bStyle}" Content="Button" Height="23" Width="75" />
</Grid>
</UserControl>
Code for "res.xaml"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="bStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
Everything is great now....Button is Red
Now lets add new Project Silverlight Application and name it "SlApp" (Silverlight 5)(namespace "test")
Lets add "uc" from "WpfLib" as link("uc.xaml" "uc.xaml.cs" ).
Create new ResourceDictionary in "SlApp" and name it res.xaml like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="bStyle" TargetType="Button">
<Setter Property="Background" Value="Blue" />
</Style>
</ResourceDictionary>
New ResourceDictionary has TargetType="Button" because x:Type is not supported in SilverLight. Background is set to color Blue not red.
And here starts the problem.
How do i make it work? Control uc displays error on merging(in Silverlight version). I need different ResourceDictionary on Wpf and Silverlight version of usercontrol. ClassLibrary doesn't support app.xaml and i can't use default style.

Related

Global style isn't being applied

I have a resource dictionary file as so:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TIMS.Common">
<local:ViewModelLocator x:Key="ModelLocator" />
<Style x:Key="DefaultWindowStyle" TargetType="Window">
<Setter Property="Background" Value="Cyan" />
</Style>
<Style x:Key="DefaultPageStyle" TargetType="Page">
<Setter Property="Background" Value="Red" />
</Style>
<Style x:Key="DefaultGroupBoxStyle" TargetType="GroupBox">
<Setter Property="Background" Value="DarkGray" />
</Style>
And it's included into my App.XAML as so:
<Application x:Class="TIMS.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TIMS.Common"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TIMS;component/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
However, this style doesn't apply unless I specifically set the style in the XAML for that object.
Example:
<Window x:Class="TIMS.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TIMS.Views"
xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=PresentationCore"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:valueConverters="clr-namespace:TIMS.Utils.ValueConverters;assembly=TIMS.Utils"
x:Name="Main"
Title="Tote Inventory Management System - Southeastern Grocers"
Width="1024"
Style="{StaticResource DefaultWindowStyle}"
Height="768"
DataContext="{Binding MainWindowViewModel,
Source={StaticResource ModelLocator}}">
How can I make these the default style without have to explicitly set them on each element ?
If you want, for example, all your windows in your app to have the same style, remove the x:Key property of your style with TargetType Window.
Having the x:Key property will force you to explicitly use the style in your windows for it to apply. If you remove the x:key, then all the Windows of your app will use that style by default.
Here is a link that explains it with more détails : https://msdn.microsoft.com/en-us/library/ms745683(v=vs.110).aspx
In short, if you want your style to be global and apply by default to all controls of the TargetType type in your app, do not set a key to your style. If you want the style to be applied only to some of the controls of the TargetType type, set a key to your style and use it explicitly on the controls that will use it.

Unable to resolve resources using separate files

I feel like this is a common sense and trivial, but i don't understand what i'm doing to begin with. I don't have any other resource I can use for help either. Sometimes i wonder if I'm even googling the question right.
I have some custom styles & templates I've made, but now the file is rather large and difficult to work with. I want to put each style or template in there own XAML files (sorta like headers/implementation files) so that a friend could work on one and then we add it to the project. (Such as Dictionary1.xaml ... ). I started a blank project to keep it simple.
Dictionary1.XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="SomeKey">
<Color x:Key="detailMark">Black</Color>
<SolidColorBrush x:Key="detailMarkBrush" Color="{StaticResource ResourceKey=detailMark}" />
<Style x:Key="flatTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Rectangle Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="1"/>
<TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
App.XAML
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainWindow x:Key="SomeKey"/>
</Application.Resources>
</Application>
And MainWindow.XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"
Height="350" Width="525">
<Grid>
<TextBox Style="{DynamicResource flatTextBox}"> <!-- doesn't autocomplete/work -->
</TextBox>
</Grid>
</Window>
Edit:
<TextBox Style="{Binding Mode=OneWay, Source={StaticResource SomeKey}}">
<!-- Throws System.Windows.Markup.XamlParseException -->
</TextBox>
As Alex mentioned, the right way to do this is using Merged Dictionaries.
Therefore, you should structure your project correctly, otherwise it will end up in a mess.
Keeping your "blank project", it should look like this:
YourProject
App.xaml (.cs)
MainWindow.xaml (.cs)
SomeOtherWindow.xaml (.cs)
Resources folder
Dictionary1.xaml
Dictionary2.xaml
...
Then you have to decide:
Do you want the resources to be available application wide?
Or do you want the resources to vary between certain windows / user controls?
If you want #1, you have to merge the dictionaries in the App.xaml file:
<Application x:Class=...
...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Dictionary1.xaml"/>
<ResourceDictionary Source="Resources/Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If you want #2, you have to merge the dictionaries in the specific window / user control file:
<Window x:Class=...
...>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Dictionary1.xaml"/>
<ResourceDictionary>
<!-- Window specific resources -->
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<!-- Content -->
</Window>

external xaml resource not found by designer when referenced from control template

I have external library libccc with its own resources defined like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ccc="clr-namespace:libccc"
>
<ccc:BooleanToHiddenVisibilityConverter x:Key="BooleanToHiddenVisibilityConverter" />
</ResourceDictionary>
In my main project I include it using ResourceDictionary.MergedDictionaries in main app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/libccc;component/Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now my problem is that when I reference that resource from Control template only like this:
<window style="{StaticResource MyDialog}">
</window>
where style is defined in application like this:
<ResourceDictionary>
<Style x:Key="MyDialog" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Label HorizontalAlignment="Center" Content="{Binding UpdateResultDescription}" Visibility="{Binding UpdateEnded, Converter={StaticResource BooleanToHiddenVisibilityConverter}, FallbackValue=Hidden}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary
VisualStudio Designer throws exception:
Exception: Cannot find resource named 'BooleanToHiddenVisibilityConverter'. Resource names are case sensitive.
My application works as expected, but designer throws exception. When I reference my libccc in other windows that do not use control template then designer works fine.
Anyone can give me a hint what can I change to make designer work?

Setting window background color with XAML

I have a standard XAML styled window in WPF (< Window ....)
In this window I insert a resource dictionary
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
In the Global.xaml dicionary I have the following code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Window">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
Nothing out of ordinary anywhere. Except it doesn't work, when I compile and run the app, the window background is shown in default white. BUT in the designer tab in Visual Studio where you can see the preview of your window, the background color is correctly changed to red. I don't understand.
I don't have any other styles inserted anywhere that could be overwriting the window's background color.
So how is it possible that in the preview tab it works correctly, but when i actually run the app, it doesn't? What am I doing wrong here?
Here is the entire window code:
<Window x:Class="Apptest.EditBook"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EditBook" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
<ResourceDictionary Source="Style/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
OK... So this is because your window is actually a type deriving from Window...
public partial class EditBook : Window { }
Target type does not yet work with derived types so you will need to add a key to the style and add it to each window you create that you want to use the style for..
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Window" x:Key="MyWindowStyle">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
then you will need to apply the style in the window...
<Window x:Class="Apptest.EditBook"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EditBook" Height="300" Width="300" Style="{StaticResource MyWindowStyle}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
<ResourceDictionary Source="Style/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
Hope this helps... there is no better solution from what I can see.

Load control style from separate file in wpf

I have the following style added to my Windows.Resources
<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0" Color="#90DDDD" />
<GradientStop Offset="1.0" Color="#5BFFFF" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
...
</Window.Resources>
I have a lot of those styles in my xaml code and I would like to save each component style to an extra file (not an external file).. for example all the styles related to TextBlocks should be in a file called TextBlockStyles.xaml
How would I do this in wpf?
How do I link the style in my project ?
Thanks in advance
You use merged resource dictionaries
In you app.xaml you would use
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
or directly into a UserControl would be
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
... other dictionaries here
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
You can shorten Source="..." to just Source="TextBlockStyles.xaml" if the file is in the same assembly and in the root of the project, or alternatively Source="Styles\TextBlockStyles.xaml" if you put the resource dictionary into the folder Styles.
Use case: you have a user control called MyView.xaml with a button. You want to style the button with an external XAML file.
In MyView.xaml:
<User Control ...namespaces...>
<UserControl.Resources>
<ResourceDictionary>
...converters...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...the rest of the control...
</UserControl>
In MyButton.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MSDNSample">
<Style x:Key="FooButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
Back to MyView.xaml ("the rest of the control"):
<Button Style="{StaticResource FooButton}">
Hello World
</Button>
In Solution Explorer Right Click on your Project Select Add After that click on Resource Dictionary...
Choose name and add to your project.
Open App.xaml
Add This Code in Application Tag
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In YourStyle.xaml :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:APPNAME">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Pink" />
</Style>
</ResourceDictionary>
you are looking for the dynamic resources. well the best way is to load and marge the dictionary in the resources. application or either on control page.
here is a good sample for it.
http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
this.Resources.MergedDictionaries.Add(Smyresourcedictionary);
Simply, go to your Window (example: MaindWindow.xaml) where you want to include the resource from the outer file and use MergedDictionaries principle to refer to that file:
<Window x:Class="UseMergedResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="600"
Width="600">
<Window.Resources>
<!-- DECLARING MERGED DICTIONARY -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source='Merged/BrushResources.xaml' />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!---------------------------------->
</Window.Resources>
<StackPanel>
<Rectangle Width='200'
Height='100'
Fill='{StaticResource PrimaryBrush}' /> <!-- USAGE HERE -->
</StackPanel>
</Window>
From above Merged/BrushResources.xaml is the location of the resource file, which is located under the folder called Merged.
Now if you are wondering what should be the declaration syntax in the outer file, check this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Location for application brushes -->
<SolidColorBrush x:Key='BorderBrush'
Color='Orange' />
<SolidColorBrush x:Key='HighLightBrush'
Color='LightBlue' />
<SolidColorBrush x:Key='PrimaryBrush'
Color='Pink' />
<SolidColorBrush x:Key='AccentBrush'
Color='Yellow' />
</ResourceDictionary>
If you want to make the resource available through all application (visible in all your windows) then declare in the App.xaml resources section.

Categories