WPF ResourceDictionary and Binding - css

I#m a beginner with WPF. I want to make the same like css/sass. I want to reuse some definitions.
Here I want to reuse a color definition in an element (e.g. button). If I use binding with "staticresource", I get the following exception when running:
"An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: "#FF8FA2AC" ist kein gültiger Wert für die Eigenschaft "Background"."
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Color x:Key="cyan3">#007ca9</Color>
<Color x:Key="mangenta2">#a8005c</Color>
<Color x:Key="wintergrey1">#e6ecf0</Color>
<Color x:Key="wintergrey2">#c3ced5</Color>
<Color x:Key="wintergrey3">#8fa2ac</Color>
<Color x:Key="wintergrey4">#506671</Color>
<Color x:Key="white">#FFFFFF</Color>
<Color x:Key="antrazit">#333333</Color>
<!-- Base style for button -->
<Style TargetType="Button" x:Key="btnStandard">
<!--Setter Property="Background" Value="#8fa2ac"/-->
<Setter Property="Background" Value="{StaticResource wintergrey3}"/>
<Setter Property="Foreground" Value="#ffffff"/>
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="30"/>
</Style>
</ResourceDictionary>
How can I use predefined definitions in other elements? Or what is wrong. I want do define 4 different button styles "Standard", "IsFocused", "IsDisabled" and "IsHero (background=mangenta2".

You should set the Background property to a Brush. If you set it to a SolidColorBrush you can then set the Color property of this one to your Color resource like this:
<Style TargetType="Button" x:Key="btnStandard">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{StaticResource wintergrey3}" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#ffffff"/>
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="30"/>
</Style>

Related

How do you provide a default Style for a Custom Control in Xamarin.Forms?

In UWP, if I want to specify a default style for a custom control, I'd code it up like this:
public PriceControl()
{
// This allows the control to pick up a template.
this.DefaultStyleKey = typeof(PriceControl);
}
I can't find the equivalent in Xamarin.Forms. How do you tell a custom control that it should use a style by default?
Just define a style in App.Xaml of the typeof your control and don't give it a key just a target type and that should do the trick:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="VerticalOptions" Value="CenterAndExpand" />
<Setter Property="BorderColor" Value="Lime" />
<Setter Property="BorderRadius" Value="5" />
<Setter Property="BorderWidth" Value="5" />
<Setter Property="WidthRequest" Value="200" />
<Setter Property="TextColor" Value="Teal" />
</Style>
</ResourceDictionary>
</Application.Resources>
Just replace button with your control and it's properties Into the setter's and this will be your global default style for this control!

does Xamarin.Forms Style support variable/parameter from applied target?

I have a complicated visual state and don't like to copy it to everywhere, so putting it in the ResourceDictionary
but some setter's value should be replaced by the real value from the target:
for example: the {Parameter1} and {Parameter1} are things I hope to replace.
<ResourceDictionary>
<Style x:Key="ToggleButton" TargetType="button:SfButton">
<Setter Property="ShowIcon" Value="True"/>
<Setter Property="IsCheckable" Value="True"/>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList x:Name="CommonStates">
<VisualStateGroup>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter Property="ImageSource" Value="{mb:ImageAsset Source={Parameter1} }" />
<Setter Property="BackgroundColor" Value="#3900" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter Property="ImageSource" Value="{mb:ImageAsset Source={Parameter2} }" />
<Setter Property="BackgroundColor" Value="#6090" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
but in the control's instantiation there's no way to pass these arguments:
<button:SfButton Style="{StaticResource ToggleButton, ??Parameter1=xxx?? }" Text="xx"/>
so is there anyway to achieve this?

How do I pick a static/dynamic resource color in a color?

How do I select a static/dynamic resource color in a color like this?
<ResourceDictionary ...
<Color x:Key="RedColor">#C3404D</Color>
...
<Color>{StaticResource RedColor}</Color>
This is the code I have
<ResourceDictionary ...>
<Color x:Key="PrimaryColor">#507EA9</Color>
<Color x:Key="SecondaryColor">#A76FBB</Color>
<Color x:Key="PrimaryDarkColor">#112951</Color>
<Color x:Key="SecondaryDarkColor">#75428F</Color>
<Color x:Key="GreenColor">#63B743</Color>
<Color x:Key="RedColor">#C3404D</Color>
<Style TargetType="chart:SfChart" x:Key="ChartPie1">
<Setter Property="ColorModel">
<Setter.Value>
<chart:ChartColorCollection>
<Color>#507EA9</Color>
<Color>#A76FBB</Color>
<Color>#112951</Color>
<Color>#63B743</Color>
<Color>#75428F</Color>
<Color>#C3F3A0</Color>
</chart:ChartColorCollection>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="chart:SfChart" x:Key="ChartPie2">
<Setter Property="ColorModel">
<Setter.Value>
<chart:ChartColorCollection>
<Color>#75428F</Color>
<Color>#63B743</Color>
<Color>#A76FBB</Color>
<Color>#112951</Color>
<Color>#C3F3A0</Color>
<Color>#507EA9</Color>
</chart:ChartColorCollection>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
I wonder if it is possible to not to have to define the colors over and over again? Just write something like:
<chart:ChartColorCollection>
<Color>{StaticResource PrimaryColor}</Color>
<Color>{StaticResource SecondaryColor}</Color>
...
</chart:ChartColorCollection>
Based on your comment you can define colors in styles as static resource and reuse them in your code. Example:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Color x:Key="PrimaryColor">#373749</Color>
<Color x:Key="SecondaryColor">#45455c</Color>
<Style x:Key="TitleLabelStyle" TargetType="Label">
<Setter Property="FontSize" Value="20" />
<Setter Property="TextColor" Value="{StaticResource PrimaryColor}" />
</Style>
</ResourceDictionary>
Then in your xaml you reference like this for example:
<StackLayout BackgroundColor="{StaticResource PrimaryColor}">
or style:
<Label Style="{StaticResource TitleLabelStyle}" />
Hope it helps.

Set DatePicker text color according to IsEnabled property

In one of my Xamarin.Forms apps I want to change the text color of a DatePicker according to the IsEnabled property.
I tried two known ways:
1) Using a style
In App.xaml:
<Style x:Key="DatePickerStyle" TargetType="DatePicker">
<Style.Triggers>
<Trigger TargetType="DatePicker" Property="IsEnabled" Value="True">
<Setter Property="TextColor" Value="Blue" />
</Trigger>
<Trigger TargetType="DatePicker" Property="IsEnabled" Value="False">
<Setter Property="TextColor" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
In View.xaml:
<DatePicker IsEnabled="{Binding IsEnabled}" Style="{StaticResource DatePickerStyle}" ... />
2) Adding the trigger in XAML
<ViewCell>
<DatePicker IsEnabled="{Binding IsEnabled}" ...>
<DatePicker.Triggers>
<Trigger TargetType="DatePicker" Property="IsEnabled" Value="True">
<Setter Property="TextColor" Value="Blue" />
</Trigger>
<Trigger TargetType="DatePicker" Property="IsEnabled" Value="False">
<Setter Property="TextColor" Value="Red" />
</Trigger>
</DatePicker.Triggers>
</DatePicker>
</ViewCell>
Both ways lead to a "System.InvalidOperationException: bindable not an instance of AssociatedType" exception.
Is it possible to change the text color of a picker with an applied style that contains a trigger at the IsEnabled property? Will a behavior be a better way to go?
I faced a similar issue with DatePicker and found no explanation on Xamarin forums or msdn. I have finally used the following workaround and it works
<Style TargetType="DatePicker">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList x:Name="CommonStates">
<VisualStateGroup>
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="TextColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
See also Visual State Manager.

How to change CommandBar Title font size in Xamarin UWP

I have created a Xamarin.Forms application and it has quite a long name.
When I start it on my 4.5" Windows 10 phone, it looks very strange.
The main page consists of a TabbedPage and it has the Title property, however it has no FontSizeproperty.
I use the following Style in my PCL project:
<Style TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource BaseColor}" />
<Setter Property="FontSize">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double"
Phone="18"
Tablet="28" />
</Setter.Value>
</Setter>
</Style>
However, if I remove it, the title is still very large.
Where can I modify the title font size to make the title smaller?
UPDATE:
I checked with the Live Property Editor, and it shows that the Title is inside the CommandBar and the FontSize is set to 24.
I tried to override its style (both in XAML and in code), but it doesn't work:
<forms:WindowsPage.BottomAppBar>
<CommandBar>
<CommandBar.Style>
<Style TargetType="CommandBar">
<Setter Property="FontSize" Value="4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Text="Whatever" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</CommandBar.Style>
<CommandBar.Content>
<TextBlock Text="Whatever" />
</CommandBar.Content>
</CommandBar>
</forms:WindowsPage.BottomAppBar>
public MainPage()
{
this.InitializeComponent();
var bapp = BottomAppBar;
LoadApplication(new MyXamarinApp.App(IoC.Get<SimpleContainer>()));
BottomAppBar = bapp;
BottomAppBar.FontSize = 4;
}
Any idea?
UPDATE 2:
You can download a sample project from here.
You have to override one of the built-in styles:
<!-- Tab title -->
<Style x:Key="TitleTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="18" />
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>

Resources