Silverlight generic.xaml - silverlight

Can I use generic.xaml in Silverlight to set the style of all TextBlock in the application?
<ResourceDictionary xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="Foreground"
Value="White" />
<Setter Property="FontSize"
Value="24" />
</Style>
</ResourceDictionary>
I was expecting this to work but it doesn't :-(
Whats the simplest way to apply styles across a whole application??
Thanks,
Mark
EDIT
Thanks for your response. I'm not getting an error, the styles just aren't being applied. All the examples that I found on the internet (including the ones you have listed) are all for styling custom controls. This I can do, but I'd like to just style the default controls:
<TextBlock Text="Style me!!" Grid.Row="2" />
Do I need to add reference to the generic.xaml from page.xaml??
Do I need to name and reference the generic.xaml style as a resource??
Thanks again, Mark

In Silverlight 4, you can use implicit styling!

Setting Styles generically/automatically by setting the TargetType and omitting a ResourceKey only works in WPF, not Silverlight.
If you want to have a Style available throughout your application you can add it to the Resources collection in your App.xaml, but to use it it will need a x:Key and you will have to apply it individually as a StaticResource binding to each instance of your target type that you want to use it.

I do believe this is in Silverlight 3. But the other guys are right, you need to style each one by hand in Silverlight 2.

Note that you could also use the ImplicitStyleManager from the Silverlight Toolkit to do this:
http://www.codeplex.com/Silverlight/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%203&referringTitle=Home
Apply the style to the top level control in your hierarchy and it will propagate down to all child controls.

What exactly doesn't work about it? Do you get an error, or do your text boxes just not get themed?
Your method is the correct way to theme controls, so just work at ironing out the problems you're having with it.
This is a good tutorial, as is this, though note that in Silverlight 2 you need to put the generic file in
themes\generic.xaml
which differs from many tutorials you might find (including the ones given above
Edit: Another tutorial here

Try something along the lines of.
<UserControl.Resources>
<Style TargetType="TextBlock" x:Name="tbStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="24" />
</Style>
</UserControl.Resources>
Then when using your TextBlock.
<TextBlock Style="{StaticResource tbStyle}" />

Related

Can an application level style reference a UserControl resource?

In my WPF application I created a resource of type SolidColorBrush and a button style which uses this resource as a background.
In my UserControl I override the color resource with the same name but a different color.
Can I create it in a way that a button in that UserControl will use the application Style but with the color defined in the UserControl?
What you are trying to do here is a little bit against the idea of resources in WPF as you try to mix up the top-down nature of resource definitions. So even if you get it to work, it will lead to untypical behavior, making it hard for the next person working on the code (or the future-you) to understand what's going on.
I would suggest to do the following to get exactly the behavior you want, still keeping things in order:
Suppose your app level resource like this:
<SolidColorBrush x:Key="MyColor" Color="Blue" />
<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource MyColor}" />
... (other setters)
</Style>
If you want to override the Foreground with a local brush, do this explicitly when using the TextBlock (might be your UserControl as well) and use BasedOn to inherit everything else from the app level style:
<SolidColorBrush x:Key="MyOverridingColor" Color="Red" />
<Style x:Key="OverridingTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockStyle}">
<Setter Property="Foreground" Value="{StaticResource MyOverridingColor}" />
</Style>
I would suggest to use a different name for the second color (MyOverridingColor in the example above) to make clear that it is a different color.

Precedence of multiple styles for the same target type?

If there are multiple styles like this:
<Style TargetType="{x:Type local:MyControl}">
...
that are merged in from resource dictionaries that target the same type of control, which should have precedence, is it the first style encountered or last one?
Where can I find the rules that govern such things?
Styles are applied from the ResourceDictionary closest to the control in question. An example:
<Window>
<Window.Resource>
<Style 1/>
<Window.Resources>
<Grid>
<Grid.Resources>
<Style 2/>
</Grid.Resources>
<TextBox/>
</Grid>
</Window>
In the above example, Style 2 will be applied to the TextBox. Should you wish to cascade the styles (apply both styles to the TextBox), you can set BasedOn on Style2 to point to Style1 using BasedOn="{StaticResource {x:Type TextBox}}". Please check the syntax, I don't have VS here.
As you can see, the type becomes the Key. Since it is not permissible to have two elements with the same key in a single ResourceDictionary, you cannot merge two ResourceDictionaries with overlapping styles. It should be possible to design around such a requirement, remembering that a ResourceDictionary can reference another use. Again, you use BasedOn.
This is the answer for your comment and for question at all. Name, x:Name doesn't play on the scene in this case. Each resource in the dictionary must have the Key. For targeted styles WPF infrastructure generates the Key, so, the styles with the same target type will have same key, thus you can't use more than one targeted style for each type in the dictionary.
It will throw an exception. This is what I tried to test this:
I wrote a simple ResourceDictionary with 2 styles, with same TargetType but without x:Key (not x:Name).
<Style TargetType="TextBox">
<Setter Property="Height" Value="100"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Height" Value="200"/>
</Style>
Rebuilt the project and it complied successfully. Now which will get applied?
Well, when I ran it, it threw a big exception when loading the styles. So in short, it doesn't work.

Xaml Grid Styling

New to WPF, I'm having some trouble creating a styles in my code, I was able to make some buttons styles by drawing rectangles and making them into buttons, this opened a template editor so I was able to do it.
Now I'm wanting to create a template for a repeating stackpanel/grid layout, and I wrote it by hand this time, but I am getting an error that says the "template is not a valid member"
This is the kind of thing I was trying to create, but the Property="Template" bit is underlined in red. Can somebody explain to me the reason behind this? How do I create or initialize the template?
<Style x:Key="LaneStyle" TargetType="{x:Type Grid}">
<Setter Property="Width" Value="760"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Grid}">
<!-- Things here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If someone could direct me to a tutorial on styles/templates that would be nice as well, haven't been able to find one that explained it in more detail.
Grid is not a control, therefore you cannot apply a ControlTemplate to it. If you're looking for a "repeater" kind of thing, you should be using an ItemsControl.
The best way to create templates/styles is by using Microsoft Blend 3.0/4.0
Over there one can easily find out what's the progress after doing each change.
In your case, a grid cannot be styled as it is a container not a control. If you wish to customize some control need to modify the control template of the control.

Setting default button style in Windows Phone 7 silverlight app

In WPF I could apply a style to a target type without giving it a name. This would effectively style all elements of that type without explicitly setting the style on each button.
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="White" />
</Style>
<Button Content="Button1"></Button>
This seems to be giving me problems within WP7 SL as the style is not applied unless I give it an x:Name and set the style on each instance.
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="BorderBrush" Value="Red" />
</Style>
Is this a chore that I will have to suffer or is there a workaround?
As far as I know, what you have observed in WP7 XAML is inherited from Silverlight. WP7 Silverlight is based on Silverlight 3 and some bits of Silverlight 4.Styles need to be explicitly applied, although this may seem to be too verbose, I personally find defining styles and using them to be best for maintenance of the code.
HTH, indyfromoz

How to change WPF Listbox/ListBoxItem DataTemplate for selected item WITHOUT affecting style & theming?

This question is very similar to Change WPF DataTemplate..., which I have read and implemented. It worked beautifully at first, but I ran into a problem with it.
That problem is that, when using themes in your application such as those in the WPF Futures project (e.g. Expression Dark), the ListBoxItems all revert back to the default WPF styling. This breaks the theme for those elements and, for example, produces black text on black background where the text would otherwise be white. This also affected my TreeView, and presumably would affect other similar controls.
I think this is because conflicting styles are being set for ListBox.ItemContainerStyle--one from the theme and one for switching data templates.
I've looked around for other solutions, but haven't found anything yet. Here are the leads or ideas I've had so far:
Subclassing DataTemplateSelector and setting it to ListBox.ItemTemplateSelector. (The current best bet).
Somehow, somewhere use a Trigger, DataTrigger, or EventTrigger.
Give up on themes.
Somehow hack the functionality I want into the theme.
Somehow make my custom ItemContainerStyle somehow inherit it's colors and eye candy from the theme's style. (I tried it briefly, and it didn't work.)
Here is my ListBox and related pieces:
<Window.Resources>
<DataTemplate x:Key="NormalTemplate">
...
</DataTemplate>
<DataTemplate x:Key="SelectedTemplate">
...
</DataTemplate>
</Window.Resources>
<ListBox x:Name="RegisterListBox" Grid.Row="0"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Adjustments}">
<!-- this is from the post referenced above -->
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContentTemplate" Value="{StaticResource NormalTemplate}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
The Listbox.DataContext is set in code to enable the ItemsSource binding.
Any ideas how I can achieve the kind of functionality described above while maintaining seamless support for themes?
Have you tried doing something like this?
<ListBox.ItemContainerStyle>
<Style
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"> <=====
...
The idea is that the framework will first go look for a style with a key equal to typeof(ListBoxItem), it will find it in the themes, and then your style will just extend the themed one with your specific details.

Resources