CalendarStyle of DatePicker different than my custom CalendarStyle - c#

I have a custom Calendar style and a custom DatePicker style.
Code in my MyCustomSkin.xaml (from my library project)
<Style TargetType="{x:Type Calendar}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="Background" Value="Green"/>
</Style>
They are both inside my MyCustomSkin.xaml ResourceDictionary (separate solution that generate a MyCustomSkin.dll) that I reference inside my application.
Code in my App.xaml (from my application)
<Application x:Class="WpfApplication1.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>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyCustomSkin;component/MyCustomSkin.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Code in my MainWindow.xaml (from my application)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<DatePicker HorizontalAlignment="Right" />
<Calendar HorizontalAlignment="Left"/>
</Grid>
</Window>
When I create a Calendar in my application (without specifying any style), the background is red.
When I am creating a DatePicker in my application (without specifying any style), the background is green but the background of its calendar is the system one (=> not red).
Why? I thougt that I was overriding all the system calendars with mine. Why is it applying the custom style for the Calendar but not on the Calendar of DatePicker?
Thank you!

Your MyCustomSkin.xaml should be:
<Style TargetType="{x:Type Calendar}" >
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type DatePicker}" >
<Setter Property="Background" Value="Green" />
<Setter Property="CalendarStyle">
<Setter.Value>
<Style TargetType="{x:Type Calendar}" BasedOn="{StaticResource {x:Type Calendar}}"/>
</Setter.Value>
</Setter>
</Style>
DatePicker has a default CalendarStyle which will be applied on component initialization.

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 override ComboBox ToggleButton from Material Design for XAML?

I'm new in WPF.
I have an issue with overriding MaterialDesignComboBoxToggleButton
style. I wanna to replace "Template" setter with own, but my content from control template always ignores. Why this occurred? With other styles i haven't this problem.
Bellow code demonstrates what i need.
Overriedes.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
App.xaml
<Application x:Class="Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-Wpf" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#FFD8E1FF" SecondaryColor="#FFD8E1FF" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="/Overrides.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Add the same resource key to your custom style like this:
<Style
x:Key="MaterialDesignComboBoxToggleButton"
BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}"
TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and you don't need to add resources like this in Overrides.xaml file:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
because the MDIX resources already included by MaterialDesignTheme.Defaults.xaml that you added in App.xaml file.
In my case override style in window not working ,but when I tried in App.xml then it works. I changed the style for toggle button by another style . the code is :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/IG/IG.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Themes/IG/IG.MSControls.Toolkit.Implicit.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border CornerRadius="8" BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF0F0F4B" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Background" Value="#FF73ADDE" />
<Setter Property="Foreground" Value="#FF150404" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Application.Resources>

Can't use XAML ResourceDictionary in a C# / WPF class library [duplicate]

This question already has answers here:
ResourceDictionary in separate library
(3 answers)
Sharing WPF-Dictionary from another assembly
(1 answer)
Closed 3 years ago.
For a Revit plugin I'm building a WPF dialog, with a view, logic, and now some styling.
Very quickly I came to the conclusion that putting all my styling in the is really messy (it just means I'm scrolling through my XAML half of my time). So I wanted to split my styling from my XAML code (like you would with HTML and CSS).
So I found out about the and how I should go about setting it up. I have my window, with this resource code:
<Window.Resources>
<local:LocationView x:Key="mainViewDataSource" />
<FontFamily x:Key="Ubuntu">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Ubuntu</FontFamily>
<FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Solid</FontFamily>
<FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Regular</FontFamily>
<ResourceDictionary x:Key="dictionary" Source="pack://application:,,,/Kalec.Enveo;component/src/WPF/Styles/Dictionary.xaml" />
</Window.Resources>
And my Dictionaries:
Dictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="MergedDictionaries">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries/InputStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
and InputStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="SearchInputStyle" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="30"/>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border CornerRadius="15" Background="White" BorderBrush="Transparent" x:Name="border">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2" />
<TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="Red"/>
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="BorderBrush" TargetName="border" Value="Blue"/>
</Trigger>
<DataTrigger Binding="{Binding Path=Text}" Value="">
<Setter Property="Text" Value="Vul een adres in"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is that I can't seem to use the style on my textBox:
<StackPanel Margin="10,10,0,0" Orientation="Vertical">
<StackPanel Panel.ZIndex="10" Orientation="Horizontal"
Name="SearchParameters"
>
<TextBox Width="220" Style="{DynamicResource SearchInputStyle}"/>
<Button FontFamily="{StaticResource FontAwesomeSolid}"
Content="" Foreground="#31B192" Grid.ColumnSpan="2"
Style="{DynamicResource LocationImport}"
Margin="10, 0, 0, 0"/>
</StackPanel>
I simply get the error that it could not be resolved. I looked at some solutions, and most of them tell me to include the ResourceDictionary in my App.xaml, but I don't have that file, because my project is a class library (it's a requirement for the plugin).
Can I even use Resource Dictionaries? Or is there some other way to seperate the styling or XAML in different files?
I guess you got the correct ingredients but not the correct recipe.
Here's how its working for me:
I have a class library project which defines all my styles (ControlLibrary in the image)
Here is one Button styles ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<Style x:Key="RedButton" TargetType="Button">
<Style.Setters>
<Setter Property="Background" Value="Red"/>
</Style.Setters>
</Style>
<Style x:Key="GreenButton" TargetType="Button">
<Style.Setters>
<Setter Property="Background" Value="Green"/>
</Style.Setters>
</Style>
</ResourceDictionary>
Then Here's is the single point of contact for all individual styles. Dictionary.xaml
(Note that this is using MergedDictionary)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="./Styles/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Finally to use the Styles from ClassLibrary dll into my WpfApp project (after adding reference to it) we again use MergedDictionaries..!!
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ControlLibrary;component/Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

WPF dynamic resource not found in MergedDictionary but outside it is fine

I have a main Class (with an MainView.xaml) that dynamically loads various other Pages long after the MainView has been shown.
So MainView.xaml has following style defined:
<Window.Resources>
<Style x:Key="ErrorTemplate" TargetType="TextBox">
<Setter Property="FontSize" Value="30"/>
</Style>
</Window.Resources>
In one of the loaded pages I had following declaration:
<Page.Resources>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</Page.Resources>
This worked fine.
Now I needed to load other resources from an external XAML file so I used MergedDictionaries.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Guaranteed to not have an ErrorTemplate key -->
<ResourceDictionary Source="MoreStyles.xaml"/>
<ResourceDictionary>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
And now I started to receive the message that ErrorTemplate could not be resolved.
Moving it outside of the MergedDictionary fixed the problem again.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoreStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</Page.Resources>
What is happening here?

Creating styles for a ListView in a WPF Desktop application

I have some styles for a Metro/win8 app:
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
But now I am making a desktop app in wpf (.net 4.5) and cannot apply styles like this in xaml to a ListView control. How do we define our own custom styles for a Desktop ListView control in xaml?
Here is an example putting the style in a windows resource dictionary.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>
and if you want the style to be placed in it's own file then you can reference that file like this (my resource file is just call Dictionary1.xaml)
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>

Categories

Resources