How do I base a style on the default style in Windows Universal apps (Win10)? - xaml

In WPF, if you want to base a style on the default style of a control, you would say:
<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource {x:Type GridView}}">
However, x:Type is not supported on UAP - how do I do it then? I tried the following - none works (after defining XAML as an alias for the namespace where GridView is).
<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource xaml:GridView}">
<Style TargetType="customControls:ResponsiveGridView" BasedOn="xaml:GridView">
None of this works - crashes on parsing the XAML.
Any more ideas?

You can still use "BasedOn" for inheritance of styles.
<Page.Resources>
<Style TargetType="Button" x:Key="MyOtherStyle">
<Setter Property="Background" Value="Red"></Setter>
</Style>
<Style TargetType="Button" BasedOn="{StaticResource MyOtherStyle}" >
<Setter Value="Green" Property="Foreground"></Setter>
</Style>
</Page.Resources>
Just define the resources like above. They'll be applied for every button on the page.
<Button Content="Hello"></Button>
To base on a default style of a control, you don't use "BasedOn". You implicitly base on the default style of a control by specifying the TargetType in the style.
To be more precise for your special case:
If you want to use an (implicit) style for your custom control that is based on a default style of a built-in control do the follwing:
Create a custom style that targets the built-in control type. Like this:
<Page.Resources>
<Style TargetType="Grid" x:Key="MyStyle1" >
<Setter Property="Background" Value="Green"></Setter>
</Style>
...
Then add another style that targets your custom control type an is based on your custom style for the built-in control. Like this:
...
<Style TargetType="local:MyCustomGrid" BasedOn="{StaticResource MyStyle1}">
<Setter Property="BorderBrush" Value="Black"></Setter>
<Setter Property="BorderThickness" Value="4"></Setter>
</Style>
</Page.Resources>
All your MyCustomGrid controls will implicitly get the style which is based on the default style.
All standard Grids will keep their default style, because they won't get the style implicitly, because you specified the x:key in the first style and therefore have to explicitly set the style of the grids. Does this clarify?

Related

How to inherit from, or override elements of, the Default style for a WinUI 3 control?

I am trying to learn how to use Styles most effectively in WinUI 3 (from WindowsAppSDK 1.1.1) but I'm having difficulty getting simple inheritance to work.
Consider the NavigationViewItem class. I'd like to modify the default style to bind the FontSize and Height properties. The following works in my Page XAML:
<NavigationViewItem x:Uid="Shell_05" helpers:NavigationHelper.NavigateTo="ViewModels._05CreditViewModel"
FontSize="{Binding ViewModel.RootShellFontSize, ElementName=shellPage}"
Height="{Binding ViewModel.CurrentMenuItemHeight, ElementName=shellPage}">
<NavigationViewItem.Icon>
<BitmapIcon UriSource="\Images\credit.png"/>
</NavigationViewItem.Icon>
</NavigationViewItem>
But adding the two properties to a page resource does not (although the FontSize property works in each of the following. It's the Height that doesn't):
<Page.Resources>
<Style TargetType="NavigationViewItem" >
<Setter Property="FontSize" Value="{Binding ViewModel.RootShellFontSize, ElementName=shellPage}" />
<Setter Property="Height" Value="{Binding ViewModel.CurrentMenuItemHeight, ElementName=shellPage}" />
</Style>
</Page.Resources>
Neither does adding the style to a resource dictionary and merging. I've read over what I can find about inheriting styles and the BasedOn="" extension is an explicit way to derive from an existing style in WinUI versions prior to 2.6 (I think). Apparently, WinUI 3 does not require BasedOn. In any case, simply specifying TargetType="NavigationViewItem" doesn't work, but nor does
<Style TargetType="controls:NavigationViewItem" BasedOn="DefaultNavigationViewItemStyle">
The source code for v1.1.1 of the SDK declares a default style for the NavigationViewItem in generic.xaml, but there is no definition for DefaultNavigationViewItemStyle.
I also cannot derive from the default style using
<Style TargetType="controls:NavigationViewItem" BasedOn="{StaticResource {x:Type NavigationViewItem}}">
because x:Type is undefined.
I can do all of the bindings I want in code but I assume it's both clearer and more efficient to do it in XAML.
How do I inherit, derive from, or override a portion of the default style for a WinUI 3 control (not a custom control) in a desktop application, please?
Thanks for any help. Pointers to good XAML for WinUI 3 documentation (or books and articles) would also be greatly appreciated.
In your case the height is most probably not working since page.resources get compiled before object initialization and the height of the CurrentMenuItemHeight is 0. To solve it just set the mode to one way as such
{Binding ViewModel.CurrentMenuItemHeight,Mode=OneWay , ElementName=shellPage}
When you wish to use BasedOn, just say BasedOn={ThemeResource styleName}.
Just make sure the style is actually defined in Generic.xaml file which u can find in "C:\Users\AdminName.nuget\packages\microsoft.windowsappsdk\1.1.1\lib\uap10.0\Microsoft.UI\Themes"
So your final page.resources should be as such:
<Page.Resources>
<Style TargetType="NavigationViewItem" >
<Setter Property="FontSize" Value="{Binding ViewModel.RootShellFontSize, ElementName=shellPage}" />
<Setter Property="Height" Value="{Binding ViewModel.CurrentMenuItemHeight, Mode=OneWay, ElementName=shellPage}" />
</Style>
</Page.Resources>
But it would be much better to use x:Bind instead of Binding. You can view this page to learn more about it https://learn.microsoft.com/en-US/windows/uwp/data-binding/data-binding-in-depth

XAML and Silverlight: Applying style to subclass fails

I have created a subclass of TextBox
public class MyAwesomeTextBox : TextBox { ... }
and have set the color of all TextBoxes to be red
<UserControl>
<UserControl.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Red" />
</Style>
</UserControl.Resources>
<TextBox ... />
<xyz:MyAwesomeTextBox ... />
</UserControl>
It works for all TextBoxes but not for MyAwesomeTextBoxes.
Please tell me what is wrong.
I bet you set the DefaultStyleKey property to typeof(MyAwesomeTextBox) and now the framework will only apply a style with the specialized stylekey.
See the documentation, I extracted the following part:
If you do not set the DefaultStyleKey, the default style for the base class is used. For example, if a control called NewButton inherits from Button, to use a new default Style, set DefaultStyleKey to the type, NewButton. If you do not set DefaultStyleKey, the Style for Button is used.
So what can you do now? You can either remove the defaultStyleKey (but that means the TextBox style will be applied everywhere in your application and you cannot have you AwesomeControlTemplate applied to it by default, so I think this is not what you should do now) or you can add a derived style to your resources:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="BaseStyle">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
<Style TargetType="MyAwesomeTextBox" BasedOn="{StaticResource BaseStyle}">
</UserControl.Resources>

Windows Phone: how to change the Application style programmatically

In my Windows Phone 8 app, I have some implicit styles defined in a xaml file at the location /Styles/DefaultStyles.xaml
I have a similar file but with different colors, fonts, etc ... defined at /Styles/GreenStyles.xaml.
I reference the default style file in my App.xaml as follows :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/DefaultStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I want to make my app switch its implicit styles from the other styles file (GreenStyles) programmatically.
How can I achieve this ?
**
UPDATE:
I manged to change the source of the resource dictionary as follows:
ResourceDictionary style = App.Current.Resources.MergedDictionaries.ToList()[0];
string source = String.Format("/ApplicationName;component/Styles/GreenStyles.xaml");
style.Source = new Uri(source, UriKind.Relative);
Note: the word component must be written like that to avoid exceptions
Now I have an issue:
only the Implicit styles (the ones that do not have a x:Key attribute) are switched when the source of the dictionary changes.
any other style with a specified key and defined twice (with different attributes) in both files, will not be reflected in the UI.
so if I have these files:
DefaultStyles.xaml:
<Style x:Key="MainGrid" TargetType="Grid">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="24"/>
</Style>
</ResourceDictionary>
And:
GreenStyles.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
<Style x:Key="MainGrid" TargetType="Grid">
<Setter Property="Background" Value="Green"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="FontSize" Value="24"/>
</Style>
</ResourceDictionary>
and I switched the source to point to GreenStyles.xaml, any Grid with the style MainGrid will still have it's background to Red.
What can be the reason for this ?
You can try using the approach Jeff Wilcox described here: http://www.jeff.wilcox.name/2012/01/phonethememanager/
Alternative approach is described here for Silverlight and I'm not sure if this will work on Windows Phone (though they share some codebase):
http://silverlightips.wordpress.com/2010/04/29/change-themestyle-using-merged-dictionaries/
Both of the ways are not easy if you have a large app and you may consider another option like (call me crazy)
<Button Style="{Binding Locator.Theme, Converter={StaticResource StyleThemeConverter}, ConverterParameter=RefreshButtonStyle}"
Hope this helps.

provide a style for textblock in the gridview

I have a gridview and there're many gridviewitems in it, every gridviewitem contains some textblock, could I specify a style for all of the textblock at once? Not specify the style one by one?
You should be able to leverage the object resources and name it implicitly. Something like;
<GridView>
<GridView.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<!-- Etc, etc, etc -->
</Style>
</GridView.Resources>
</GridView>

Giving a specific style to DataGrid element (implicitly)

I'm trying implicitly apply a style for DataGrid and TextBlocks.
For TextBlock's ForeGround I need White color.
For DataGrid's rows I need Black Color.
Beside this I need White again for DataGrid's header columns.
When I globally apply an implicit style for on MainPage by
<UserControl>
<UserControl.Resorces>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="White"/>
</Style>
</UserControl.Resorces>
</UserControl>
Making TextBlock's Foreground White operation is done! But beside this all of elements
in DataGrid (By default content elements are textblock's I think) turn to White color.
It doesn't look good White on white as you guess :)
So how can I particularly specify DataGrid's elements Foreground to black?
I can do it by using same technic shown below ,but this is an expensive operation for each DataGrid. As a con more I want DataGrid's HeaderColumns white again.This operation make them all black.
Is there an explicit way such as we do in css styles?
Here is what I tried to achieve this goal by Control template. But no chance because of being DataGrid's ContentControl is dynamic.
<DataGrid>
<DataGrid.Resources>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="Black"/>
</Style>
<DataGrid.Resources>
In fact we use Telerik's RadGridView but I give a sdk's DataGrid example to make question more global.
<Style TargetType="sdk:DataGrid">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="RowDetailsTemplate" Value="{StaticResource DataTemplate1}"/>
<Setter Property="Template" Value="{StaticResource ControlTemplate1}"/>
</Style>
<ControlTemplate x:Key="ControlTemplate1" TargetType="sdk:DataGrid">
<Grid/>
</ControlTemplate>
<DataTemplate x:Key="DataTemplate1">
<Grid/>
</DataTemplate>
Thanks in advance!
If it were me I would pull out the full control templates and style them accordingly instead of trying to just do adhoc setter changes to override bits of the original template. In Expression Blend right click, choose "Edit Template -> Edit A Copy" and break out the templates for your rows etc and apply those implicitly with StaticResource instead.