I want to build up styles in XAML, e.g.
<UserControl.Resources>
<Style x:Key="MyStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="Foreground" Value="DarkRed" />
</Style>
<Style x:Key="MyBoldStyle" TargetType="TextBlock">
<Setter Property="Style" Value="{StaticResource MyStyle}" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</UserControl.Resources>
But this does not work. (In Silverlight 2) How can I make one inherit from the other?
June 2010 Update: Style Inheritance is in Silverlight 3, use BasedOn="{StaticResource MyStyle}
Nevermind. I found the answer in MacDonald's Pro Silverlight 2 in C# 2008:
(source: apress.com)
"If you've used styles in WPF, you'll find
that Silverlight styles are
dramatically scaled back ... [for example, you can't]
create styles that inherit from other styles."
Too bad. Maybe in Silverlight 3.
Related
We're building a Xamarin Forms app we've noticed we could style an element in 2 ways by creating styles in the App.xaml ResourceDictionary
Class and StyleClass option
In App.xaml we'll write
<Style Class="EntryStandard" TargetType="Entry">
<Setter Property="TextColor" Value="#575e62" />
<Setter Property="BackgroundColor" Value="#9facb3" />
<Setter Property="FontSize" Value="14" />
</Style>
Then this gets used in one of the contentpages like this
<Entry StyleClass="EntryStandard" Placeholder="Login Name" Text="{Binding EntryEmailAddress}" />
Key and Style option
This is what we write under App.xaml
<Style x:Key="ButtonMainMenu_Purple" TargetType="Button">
<Setter Property="BackgroundColor" Value="#5d4785" />
<Setter Property="FontSize" Value="14" />
<Setter Property="TextColor" Value="#FFFFFF" />
</Style>
And then we use the following in our contentpages
<Button Style="{StaticResource ButtonMainMenu_Purple}" Text="Friends" Command="{Binding OnFriendsButtonCommand}" />
Both work fine, I just wanted to know which one is better than the other and why?
Regular styles follow the standard, relatively inflexible WPF model. Style classes includes cascade semantics and are part of the new theme support. They are poorly documented, however, and still in beta.
I am using WinRT XAML Toolkit in Windows Universal Application C#.
I want to change distance between plus(+) and minus(-) buttons and increase size of both buttons.
There are templates available for WPF but not for Universal app.
Here is image
Please advice me how can I achieve it?
The template is in the toolkit here.
You might be able to get what you want by changing these button styles to add the Margin:
<!-- DecrementButtonStyle -->
<Style
x:Key="DecrementButtonStyle"
BasedOn="{StaticResource NumericUpDownButtonStyle}"
TargetType="RepeatButton">
<Setter
Property="Content"
Value="➖" />
<Setter
Property="Margin"
Value="5" />
</Style>
<!-- IncrementButtonStyle -->
<Style
x:Key="IncrementButtonStyle"
BasedOn="{StaticResource NumericUpDownButtonStyle}"
TargetType="RepeatButton">
<Setter
Property="Content"
Value="➕" />
<Setter
Property="Margin"
Value="5" />
</Style>
I have recently started a Xamarin Forms project specifically for the Universal Windows Platform. In attempt to add some global styles for the application, I have come across an element I don't know the uwp equivalent syntax for: Xamarin ContentView.
(ie. I know that Xamarin Label = UWPTextBlock, Xamarin HorizontalOptions = UWPHorizontalAlignment)
Xamarin:
<Style x:Key="errorContainer" TargetType="ContentView">
<Setter Property="BackgroundColor" Value="Red"/>
<Setter Property="HorizontalOptions" Value="Fill"/>
<Setter Property="Padding" Value="15"/>
</Style>
UWP:
<Style x:Key="errorContainer" TargetType="?????????">
<Setter Property="Background" Value="Red"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="15"/>
</Style>
tldr; Xamarin ContentView = UWP ??????
Thanks for the help
ContentControl (and it's derived types like Border) should fit your need.
(Because it has a ContentTemplate Property, as Xamarin.Forms' ContentView derives from TemplatedView.)
I have a windows 8.1 app.
In Project.Shared there is ResourceDictionary SharedResources.xaml with base style,
<Style x:Key="CommonLayerListItemStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe WP" />
<Setter Property="Foreground" Value="{StaticResource UnselectBrush}" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
in Windows Phone app is StyleResources.xaml wiht style based on this
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
same in Windows app StyleResources.xaml:
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="28" />
<Setter Property="Margin" Value="42,0,0,0" />
</Style>
All styles are used in UserControl created in Shared project.
I do this to override FontSize on diffrent platforms.
I merged all dictionaries in App.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/SharedResources.xaml" />
<ResourceDictionary Source="Styles/StyleResources.xaml" />
</ResourceDictionary.MergedDictionaries>
But my app does not start with Unhandled exception
Cannot find a Resource with the Name/Key CommonLayerListItemStyle [Line: 10 Position: 37]
Why does this happens?
I had the same issue. You need to merge the SharedResources.xaml directly into StyleResources.xaml, and only reference StyleResources.xaml from App.xaml.
Here is a complete example:
http://blog.craftingbytes.com/2015/05/resource-sharing-in-windows-universal.html
In Windows 8, you were able to create your own themes for your application (here's a tutorial).
In Windows 8.1 Applications, themes are handled differently: you can change them at run-time and set a theme for a specific control in your XAML (if you don't want to apply the theme to the whole Application).
For instance:
<Grid x:Name="MainGrid" RequestedTheme="Dark">
However, I could not find a way to create my own themes. The property RequestedTheme takes an enumeration (its type is FrameworkElement.RequestedTheme) and an enumeration by definition cannot be extended (in C#). Also, if I want to define a new Theme Dictionary I would have written:
<ResourceDictionary.ThemeDictionaries>
But it is not available in Windows 8.1.
How can one create a theme in Windows 8.1? Am I limited to the existing ones (light & dark)?
Yes you're restricted to 3 themes I believe
Default (light)
Dark
High Contrast
You can create new styles or override existing ones for the 3 themes like this in 8.1
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="30" />
<Setter Property="Foreground" Value="Orange"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Blue"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>