how to load custom staticresource - c#

I have to 2 files:
ButtonStyle.xaml and
ConstantsStyle.xaml
in App.xaml, i will init
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ConstantsStyle.xaml"/>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......
</Application.Resources>
file ConstantsStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Thickness x:Key="GenericButtonStylePadding">0,7</Thickness>
</ResourceDictionary>
file ButtonStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:usercontrols="clr-namespace:Zalo.UserControls">
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="{StaticResource GenericButtonStylePadding}"/>
</Style>
</ResourceDictionary>
App crash runtime. Because Cannot find a Resource with the Name/Key GenericButtonStylePadding???
How can i run app correctly??? Please help me

Try using DyanamicResource instead of StaticResource
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="{DynamicResource GenericButtonStylePadding}"/>
</Style>
Edit:
You can test one thing. Add another constant to Constant.XAML
<Brush x:Key="BGBrush">Black</Brush>
Try to use it in your ButtonStyle.XAML
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Background" Value="{StaticResource BGBrush}"></Setter>
</Style>
See if button background changes to Black.
If color is changing then try something else than Thickness.

Related

Style was not found XAML

So I have my App.xaml :
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Fitness_App.App">
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WorkoutStyle" TargetType="Label">
<Setter Property="FontSize" Value="28"/>
<Setter Property="FontFamily" Value="BebasNeue"/>
<Setter Property="TextColor" Value="Wheat"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Here I declared a style GLOBALLY as far as I've read the microsoft documentation.However , when in another xaml file I do this:
<Label Style= "{StaticResource WorkoutStyle}"
It says that WorkoutStyle could not be found.Any idea why?
Declaration is correct. Just Remove obj and bin of targeted project and do a rebuild.
This will solve the issue.
This is working
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WorkoutStyle" TargetType="Label">
<Setter Property="FontSize" Value="28"/>
<Setter Property="TextColor" Value="Wheat"/>
</Style>
</ResourceDictionary>
</Application.Resources>
This line is not good
<Setter Property="FontFamily" Value="BebasNeue"/>
Your FontFamily is wrong look here for an example , change it with your font
<Style x:Key="NormalFont" TargetType="Label">
<Setter Property="FontFamily" Value="NunitoSans-Regular.ttf#NunitoSans-Regular.ttf"/>
</Style>
Explaned here
https://www.serkanseker.com/xamarin-forms-custom-font-family/

How to repeat programming lines in XAML?

... by the term "programming lines" I mean parts of code .
I am currently creating an UI using c# and XAML. But the XAML code is getting longer and longer, so I realised that if I could somehow set inside the code or store separately, repeatable parts of code and use them every time I needed, the whole XAML code would be shorter and clearer.
For example, let's say that I have a specific label which I want to repeat in several points of the code:
<Label Name="myLabel" Content="something">
</Label>
How could I possibly apply and repeat that label inside my XAML code?
There is a quick example of how the XAML code can be shared between different views/windows. Create a ResourceDictionary, define the shared properties/styles/control templates, like this
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="Label" x:Key="TitleStyle" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="FontSize" Value="16" />
</Style>
</ResourceDictionary>
Than you can add this dictionary to App/Window MergedDictionaries to use them, like
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Please, note that this is just a quick example to briefly explain the idea. You can also have look at Style.TargetType docs to see explanation between TargetType and x:Key in styles

How to change all element's font family wpf-xaml

I use MetroDark theme.
I use this code in xaml :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Toolkit.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Label}" >
<Setter Property="FontFamily" Value="B titr" />
<Setter Property="FontSize" Value="13" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style TargetType="{x:Type TextBox}" >
<Setter Property="FontFamily" Value="B Nazanin" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
</Application.Resources>
font family was changed correctly but the background color was changed too.
I just want to change the font family and font size, and another property (such as: background, border etc) and get effect from default theme (MetroDark).
How can I do that?
set base style in BasedOn attribute like this:
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
this way custom style inherits all settings from base style and can override some of them

Changing Windows.Ribbon background color

I am working with the System.Windows.Ribbon in my project. I am also using some other libraries like AvalonDocking,... What I want to do is create my own themes inside the application so that the user can select the prefered theme.
The problem is that I don't get the RibbonTab to change to the correct colors. When I change the ribbon background color the RibbonTab color changes also. But I want to change it seperatly
Does anybody have any experience with changing the layout of the System.Windows.Ribbon?
This is what I tried before:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock">
<SolidColorBrush x:Key="WindowBrush" Color="Black"/>
<Style TargetType="{x:Type Ribbon}">
<Setter Property="Background" Value="#444444" />
<Setter Property="Foreground" Value="White" />
</Style>
<Style TargetType="{x:Type RibbonTab}">
<Setter Property="Height" Value="88" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Background" Value="Black" />
</Style>
<Style TargetType="{x:Type xcad:DockingManager}">
<Setter Property="Background" Value="#444444" />
<Setter Property="Foreground" Value="White" />
</Style>
</ResourceDictionary>
As you can see, the RibbonTab is not black like specified inside the ResourceDictionary

How to set attributes without overriding the Mahapps theme?

I'm using Mahapps for a GUI, however I want to set some attributes different than visual ones such as margins and verticalAlignment, so I added this to the UserControl.resources section
<Style x:Key="{x:Type TextBox}" TargetType="TextBox" BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
However it overrides all the visual styles attributes of the TextBoxes, how can I just add those attributes without overriding all the visual styles settings?
give the style a key
<Style x:Key="myCustomTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
and use it where you need it
<TextBox Style={StaticResource myCustomTextBoxStyle} />
EDIT
or put it to the main resource dictionary of user control or window resource without a key
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ResourceDictionary>
</Window.Resources>
hope that helps

Categories

Resources