XAML WinRT - Factory Pattern for Custom Styles - c#

I'd like to implement a sort of Factory Pattern for XAML. I created an app for WinRT where I defined two xaml style files. Basically, what I'd like to achieve (if possible) is to load one of the the two xaml file when the application starts.
in the solution explorer I have this:
CustomStyles foder contains the style files. So, based on an enumerator in my App.xaml.cs file
public enum Style
{
Style_1,
Style_2
}
if I choose Style_1 I'd like to load the xaml file Style_1.xaml else Style_2.xaml at run-time.
Both the style files, have the same definition of Button style, TextBlock style, etc with different property values.
Here an example:
Style_1.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Foreground" Value="#78CAB3" />
<Setter Property="FontSize" Value="15" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
Style_2.xaml
<Style x:Key="Attribute_Label" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Foreground" Value="#606060" />
<Setter Property="FontSize" Value="30" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
There is a way to achieve what I want to do ? Thank you in advance.

We end up to do something like this:
defining a ResourcesDictionary in App.xaml with all the CustomStyles
we make a server request that decides which custom style has to be loaded
using this piece of code Application.Current.Resources[CustomStyleVariable]; we load the whole style in a Style object.
We haven't found any better solution yet but, it seems to work.

Related

How to use <FontFamily> to reference resource fonts?

I added a font file to the project, and I want to put it in the resource dictionary.
But it can't be identified correctly. I think it can only recognize the font name.
code
<FontFamily x:Key="anticon-uri">pack://application:,,,/Ant.WPF;component/Resources/#anticon</FontFamily>
I've tried the following method, it's OK.
```
<Style x:Key="anticon" TargetType="TextBlock">
<Setter Property="FontFamily" Value="pack://application:,,,/Ant.WPF;component/Resources/#anticon" />
</Style>
<Style BasedOn="{StaticResource anticon}" TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
```
Do you have a better way?
I just found the answer.
syntax: <FontFamily x:Key="anticon-uri">ResourceFile FontName</FontFamily>
Example:
<FontFamily x:Key="anticon-uri">pack://application:,,,/Ant.WPF;component/Resources/#anticon anticon</FontFamily>

C# WPF Xaml: Globally set all text in a view to one color, and all backgrounds to another

Is there a way (using data binding or simply xaml) to set the background of all elements in a view to one color, and all text to another?
I know I can edit each element in the view one by one, but I I'd like to see if this is possible with settings at a global level. Kind of like how everything by default is set to black on white.
I guess what I'm asking is if there is a feature/setting of a WPF application that offers what I'm looking for, and/or what I should search to find an answer online.
My project isn't using anything but what visual studio offers when you create a WPF project, so I can't use a prism or mvvm light approach.
Thanks in advance for your answer!
Globally...or simply XAML...
if there is a feature/setting of a WPF application that offers what I'm looking for
In Application Resource add style like this:
<Style TargetType="Control">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
Based on your target element you want to set background.
When you say "the background of all elements in a view" you should be more specific, If by 'element' you mean UIElement then there is no Background property in UIElement. If it means Control then not all UIElementsderive from Control (e.g. TextBlock) and finally if it means every UIElement derived type defined in your view that have a Background property, then you should add different styles for each type without setting the x:key to the YourView.Resources like this:
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
...
</Window.Resources>
uses controls collection through which you can control all controls in WPF

Add ErrorTemplate to a MahApp control without overwriting its default style

I am using MahApps and working on implementing validation for TextBoxes. MahApps provides some nice properties in TextBoxes, as Controls:TextBoxHelper.Watermark and Controls:TextBoxHelper.ClearTextButton. I am writing an ErrorTemplate using my style but I overwrite the default template of the TextBox and lose those Metro properties. How can I achieve my goal without losing the template:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorToolTipTemplate}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
You should define your new style based on existing to keep everything it has. I guess in case of MahApps it will be MetroTextBox:
<Style TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}">
<!-- your properties go here -->
</Style>
The BasedOn is the key to success. However, it's likely, that you're using MahApps controls not in just one place. Therefore, try to make your style more generic and avoid direct references to MetroTextBox resource in the BasedOn.
The more good looking XAML style would be like that:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- your properties go here -->
</Style>

The Style object cannot affect the Style property of the object to which it applies

I have two styles
<Style x:Key="FontElemNivel1">
<Setter Property="TextElement.FontSize" Value="12"/>
<Setter Property="TextElement.FontFamily" Value="Tahoma"/>
<Setter Property="TextElement.FontWeight" Value="Bold"/>
</Style>
And this
<Style x:Key="ElementoNivel1" TargetType="TextBlock">
<Setter Property="Style" Value="{StaticResource FontElemNivel1}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
If I try to use the second on an object like this
<TextBlock Text="Entidad" Style="{DynamicResource ElementoNivel1}"/>
The compiler throw this error:
Error 16 the Style object cannot affect the Style property of the object to which it applies.
Why this happens ?
How to implement it properly ?
<Style x:Key="ElementoNivel1" TargetType="TextBlock" BasedOn="{StaticResource FontElemNivel1}">
This should rectify an error. You tried to assign style to style.
For BasedOn to work FontElemNivel1 will need to target a Textblock.
If doesn't suit because FontElemNivel1 needs to be used for something other than textblocks then maybe this previously answered question will help:
XAML Combine Styles

Textbox style trigger with "IsReadOnly" not working

I've made an application resource with a style which should be triggered if the textbox has the "IsReadOnly" property. Looks like this:
<Application.Resources>
<Style TargetType="{x:Type TextBox}" >
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Trigger.Setters>
<Setter Property="Background" Value="Black" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
However, the program doesn't react to this. It works when I use IsEnabled=True. However IsEnabled=False doesn't work either. So, question: Do the triggers only work if you check for "True"? And is IsReadOnly not supported at all? If so: How do I know which control properties are actually supported?
See an answer for this problem by following the provided link:
DataTrigger problem with textbox

Categories

Resources