How to use multiple key(used in ResourceDictionary) in Style="{StaticResource
TopHeader }"
You can combile your multiple styles inside new created style.
For example:
<Style x:Key="Style1" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
</Style>
<Style x:Key="Style2" TargetType="Button">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style x:Key="Style1Style2" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Red" />
</Style>
or you can create BasedOn style:
<Style x:Key="Style3" TargetType="Button" BasedOn="{StaticResource Style2}">
<Setter Property="Background" Value="Yellow" />
</Style>
Related
Don't ask me why, but I thought only <ContentPage> can have <ContentPage.Resources>.
So I had a bunch of color setters and what not in almost every XAML page like this:
<ContentPage.Resources>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="#000" />
<Setter Property="BarTextColor" Value="#20b8a2" />
</Style>
<Style TargetType="ContentPage">
<Setter Property="BackgroundColor" Value="#231f20" />
</Style>
<Style TargetType="Label">
<Setter Property="TextColor" Value="#f7f7f7" />
</Style>
<Style TargetType="Entry">
<Setter Property="TextColor" Value="#f7f7f7" />
</Style>
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="#0089c1" />
<Setter Property="TextColor" Value="#f7f7f7" />
<Setter Property="CornerRadius" Value="0" />
</Style>
</ContentPage.Resources>
So how to change all main colors in one place?
So instead of having resources in every Solution/App/Views/SubPage.xaml file there is obviously an outermost XAML file in general projects root Solution/App/App.xaml where you can simply set these resources for entire application.
<Application ...>
<Application.Resources>
<Color x:Key="PrimaryColor">#20b8a2</Color> <!-- turqoise -->
<Color x:Key="SecondaryColor">#0089c1</Color> <!-- blue -->
<Color x:Key="TertiaryColor">#ef569f</Color> <!-- pink -->
<Color x:Key="ThemeExtremeColor">#000000</Color>
<Color x:Key="ThemeMainColor">#231f20</Color>
<Color x:Key="ThemeLighterColor">#333333</Color>
<Color x:Key="TextColor">#f7f7f7</Color>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="{StaticResource ThemeExtremeColor}" />
<Setter Property="BarTextColor" Value="{StaticResource PrimaryColor}" />
</Style>
<Style TargetType="ContentPage">
<Setter Property="BackgroundColor" Value="{StaticResource ThemeMainColor}" />
</Style>
<Style TargetType="Grid">
<Setter Property="BackgroundColor" Value="{StaticResource ThemeMainColor}" />
</Style>
<!--<Style TargetType="ListView">
<Setter Property="BackgroundColor" Value="{StaticResource ThemeMainColor}" />
</Style>-->
<Style TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource TextColor}" />
</Style>
<Style TargetType="Entry">
<Setter Property="TextColor" Value="{StaticResource TextColor}" />
</Style>
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="{StaticResource SecondaryColor}" />
<Setter Property="TextColor" Value="{StaticResource TextColor}" />
<Setter Property="CornerRadius" Value="0" />
</Style>
</Application.Resources>
....
</Application>
Now "{StaticResource TextColor}" can be used anywhere in XAML.
I recently downloaded Wox, an alternative search for Windows. Its search automatically appears as a dropdown menu. There is no way in the settings to change this to "dropup" rather than dropdown. I couldn't find this question asked anywhere, but the maker said in response to a question about a different change to appearance to edit the base theme document. I looked through the document, and couldn't find anywhere that seemed like an obvious place to toggle if the dropbox falls up or down, but I also only have very basic coding knowledge. I tried to search for how to do this online, but couldn't seem to find anything. The markup document is below. Is there anything I can change in this to cause it to drop up instead?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<Style x:Key="BaseQueryBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="28" />
<Setter Property="FontFamily" Value="Segoe UI, Microsoft YaHei" />
<Setter Property="FontWeight" Value="Medium" />
<Setter Property="Height" Value="30" />
<Setter Property="Background" Value="#616161" />
<Setter Property="Foreground" Value="#E3E0E3" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
</Style>
<Style x:Key="BaseWindowBorderStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="CornerRadius" Value="0" />
<Setter Property="Background" Value="#424242"></Setter>
<Setter Property="Padding" Value="4 4 4 4" />
</Style>
<Style x:Key="BaseWindowStyle" TargetType="{x:Type Window}">
<Setter Property="Width" Value="350" />
<Setter Property="MaxWidth" Value="800" />
</Style>
<Style x:Key="BasePendingLineStyle" TargetType="{x:Type Line}">
<Setter Property="Stroke" Value="Blue" />
</Style>
<!-- Item Style -->
<Style x:Key="BaseItemTitleStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FFFFF8" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Medium" />
<Setter Property="FontFamily" Value="Segoe UI, Microsoft YaHei" />
</Style>
<Style x:Key="BaseItemSubTitleStyle" TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="#D9D9D4" />
<Setter Property="FontFamily" Value="Segoe UI, Microsoft YaHei" />
</Style>
<Style x:Key="BaseItemNumberStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="3 0 0 0" />
<Setter Property="FontSize" Value="22" />
</Style>
<Style x:Key="BaseItemTitleSelectedStyle" TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="#FFFFF8" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Medium" />
</Style>
<Style x:Key="BaseItemSubTitleSelectedStyle" TargetType="{x:Type TextBlock}" >
<Setter Property="Foreground" Value="#D9D9D4" />
</Style>
<Style x:Key="BaseListboxStyle" TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer Focusable="false" Template="{DynamicResource ScrollViewerControlTemplate}">
<VirtualizingStackPanel IsItemsHost="True" />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ScrollViewer Style -->
<ControlTemplate x:Key="ScrollViewerControlTemplate" TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!--content in the left of ScrollViewer, just default-->
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
CanContentScroll="{TemplateBinding CanContentScroll}"
CanHorizontallyScroll="False"
CanVerticallyScroll="False"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Grid.Column="0"
Margin="{TemplateBinding Padding}"
Grid.Row="0" />
<!--Scrollbar in thr rigth of ScrollViewer-->
<ScrollBar x:Name="PART_VerticalScrollBar"
AutomationProperties.AutomationId="VerticalScrollBar"
Cursor="Arrow"
Grid.Column="1"
Margin="3 0 0 0"
Maximum="{TemplateBinding ScrollableHeight}"
Minimum="0"
Grid.Row="0"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}"
Style="{DynamicResource ScrollBarStyle}" />
</Grid>
</ControlTemplate>
<!-- button style in the middle of the scrollbar -->
<Style x:Key="BaseThumbStyle" TargetType="{x:Type Thumb}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border CornerRadius="2" DockPanel.Dock="Right" Background="#616161" BorderBrush="Transparent" BorderThickness="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="BaseScrollBarStyle" TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false" />
<Setter Property="Stylus.IsFlicksEnabled" Value="false" />
<!-- must set min width -->
<Setter Property="MinWidth" Value="0"/>
<Setter Property="Width" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<DockPanel>
<Track x:Name="PART_Track" IsDirectionReversed="true" DockPanel.Dock="Right">
<Track.Thumb>
<Thumb Style="{DynamicResource ThumbStyle}"/>
</Track.Thumb>
</Track>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Here is an MSDN example for changing ComboBox's ControlTemplate: https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/combobox-styles-and-templates
The interesting part is Popup:
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
Changing Placement to Top should help.
I have a user control with defined resources.
Here is some code for illustration.
<UserControl.Resources>
<SolidColorBrush x:Key="foregroundColor" Color="Red"/>
<Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
<Setter Property="Foreground" Value="{Binding ???}"></Setter>
</Style>
<Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="{Binding ???}"></Setter>
</Style>
</UserControl.Resources>
Now, I wish to use value defined in foreground color for buttonFontIconStyle, menuItemLabelStyle (and many others). Is it somehow possible to bind to value from resources in resources, or is there a way to specifiy color once (in xaml preferrably) and use it in multiple resources styles?
You can use the StaticResource :
<Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
<Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>
<Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>
I have xaml styles that have different target types but are otherwise identical. Is there a way I could cut out the duplication and define the style only once?
<Style TargetType="TextBlock">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Height" Value="{StaticResource ElementHeight}"/>
<Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
<Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
You could use style inheritance with the help of Style.BasedOn.
First define the base style:
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<Setter Property="Height" Value="80"/>
<Setter Property="MinWidth" Value="80"/>
<Setter Property="Margin" Value="80"/>
</Style>
Then "inherit" styles from that for the controls you want:
<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}"/>
Is it possible to set Foreground property from the Style? Looking like it has no effect.
<Style x:Key="MyPageNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}" />
<Setter Property="Margin" Value="0,12,0,0"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
It's simple be sure you bind it to a static resource
<Grid.Resources>
<Style x:Key="MyPageNameStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}" />
<Setter Property="Margin" Value="0,12,0,0"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
</Grid.Resources>
<TextBlock Style="{StaticResource MyPageNameStyle}" Text="WP8 Demodccxzcxzczsczczxcxzczczczcz" Margin="9,-7,0,0" />
Youll be able to see the effect in the xaml designer only