Silverlight: Change style for state - silverlight

My App.xaml looks like this:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="mySilverlightApp.App"
>
<Application.Resources>
<Style x:Name="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="FontFamily" Value="Calibri.ttf#Calibri"></Setter>
</Style>
</Application.Resources>
As you can see, I'm trying to apply custom styling with <Setter> tags to ComboBoxes in my app. But I want to apply styling for different states of the ComboBox (MouseOver, etc).
How can I do this?

To accomplish this, you'll have to create a control template in your implicit style. Something like the following:
<Style x:Name="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
....
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You can refer to this link for the default styles/controltemplates for the combobox: http://msdn.microsoft.com/en-us/library/dd334408(v=vs.95).aspx
A great way to get started re-templating controls is using Blend. You can right click a control you have dragged onto the artboard and use the "Edit Template -> Edit a Copy" command. This will create a default controltemplate for you (the same one shown in the link I provided).
From there you can edit the mouse-over state in Blend by using the States tab.
There's alot going on here, but this should get you started on the right path.

Related

MahApps Dialog overriding Button style?

I'm using the MahApps metro toolkit, and have the following inside my Apps.xaml file:
<Application.Resources>
<ResourceDictionary>
...
<Style TargetType="{x:Type Button}" />
...
</ResourceDictionary>
</Application.Resources>
Which nicely overrides the MahApps metro style for Button back to 'default'.
However, I've written a custom dialog (called DialogEditGroup - which derives from CustomDialog), but weirdly the <Button /> style has reverted back to using some form of styling from the MahApps metro toolkit.
The only way I've been able to reset it again, is by adding the following lower down in my Apps.xaml ResourceDictionary:
<Style TargetType="{x:Type controls:DialogEditGroup}">
<Style.Resources>
<Style TargetType="{x:Type Button}" />
</Style.Resources>
</Style>
I really don't want to have to add this in for every custom dialog I write, and I'm sure the above 'fix' isn't the correct way of solving the problem. Can anyone suggest a proper way of addressing this issue?
`
(Top of image is 'mahapps', bottom is 'default / generic')

WPF Styles and Polymorphism

I know I'm a pervert, but I am very curious, is there a way to make custom controls to seek for it's base class style first, and then it's own.
Why I'm asking: I have some TreeView derived controls with custom item templates. I apply those templates, then I have a base style. Later I might apply some color palette. At the last step I have a problem. I need to apply 2 styles. PVStructuralTree is derived from TreeView it has some DependencyProperty DataTemplates that get inserted into resources in code.
PVStructuralTreeView
EmploeeTemplate
... more templates
Default style for PVStructuralTreeView:
<Style x:Key="DefaultPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultTreeView}">
<Setter Property="EmploeeTemplate"><!-- This get inserted inro Resources in code -->
<Setter.Value>
<DataTemplate DataType="{x:Type s:Emploee}">
...
</DataTemplate>
</Setter.Value>
</Setter>
... Lots of them here
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}"/>
Default style for a TreeView (it's pretty big, so I won't post it here):
<Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}"/>
In color template.xaml file I'd like to have this + some magic to apply both styles at the same time (from Generic.xaml and themed one):
<Style x:Key="ThemedTreeView" TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}">
...
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedTreeView}"/>
But it just overwrites generic.xaml styles. I want it to add to it.
Now I'm doing this way:
<Style x:Key="ThemedPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}">
... CopyPaste from ThemedTreeView ...
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedPVStructuralTreeView}"/>
Does anyone knows the way how to reuse the ThemedTreeView style here?
You can base a Style on Another (one only!) Style using the BasedOn property and override specific properties, but you cannot base a DataTemplate or a ControlTemplate on another template. This is not supported. A template must be defined as a whole:
WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?

What is the most economical way to implement your own window border and title bar?

I am pretty new to WPF and am sitting here with my book trying to figure out the best approach to this application.
The title bar is not part of the client area so I am making my own title bar.
Which way would it be easiest to make this into some sort of resource to apply to all new windows I create?
<Application.Resources>
<Style x:Key="WindowTheme">
<Setter Property="Window.WindowStyle" Value="None"/>
</Style>
<!--Would I create a user control here for the title bar/border and title bar buttons? Or would it be a style?-->
</Application.Resources>
In WPF, there are two ways to use styles: Named styles and typed styles. A named style has an x:Key="..." attribute. A typed style doesn't have a name, but a TargetType="..." attribute (Rem: Named styles can and very often do have a TargetType as well, so named styles and unnamed styles would be more precise). Typed styles automatically get applied to all controls in the scope, which are of type TargetType (not a derived type).
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Blue" />
</Style>
To create your own window, you can set it's template property to a UserControl in the style:
<Style TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The professional way to implement the control template is to implement it 'from scratch', this means not using a UserControl which derives from Window. To do this, you define the visual tree of the Window, and use the WPF feature TemplateParts to define what part of your control template is responsible for what functionality of the window.
Here is a tutorial which describes pretty exactly what you want to do:
CodeProject tutorial

silverlight 3.0 grid row selector color

is it possible to specify the color for the row selector in silverlight grid
Yes but you need to copy the control template for the DataGridRowHeader control and place it in a Style object in a resource:-
<UserControl.Resources>
<Style x:Key="CustomRowHeader" TargetType="DataGridRowHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="localprimitives:DataGridRowHeader">
<!-- Copy of the rest of the standard controls template -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<UserControl.Resources>
<DataGrid RowHeaderStyle="{StaticResouce CustomRowHeader}" ... >
Now you can fiddle around with the color value and pretty much anything else that is used to render the row selector.
You can probably do this with Blend fairly well if you have it and are familiar with using it. I prefer to copy the template from the documentation. See DataGrid Styles and Templates
No, but it's perfectly possible to subdivide a grid into rows/columns and fill them with rectangles+background or something like that.

Do custom controls in Silverlight 4 need to be in separate DLL's?

I am creating a number of custom controls for a Silverlight 4 project. I have successfully created one control and am wondering if I can define more in the same project and then have all the controls bundled into one .DLL.
Right now, I have the standard files for the first control:
/Resources/icon.png
/themes/generic.xaml
/CustomControl1.cs
/CustomControl1EventArgs
I am thinking it's not possible, as there can only be one "generic.xaml".
Thanks,
Scott
Yes you can create multiple controls in the same project, you simply have to place the all the default templates in a single /themes/generic.xaml file. Each controls template is identified by the TargetType. So your generic.xaml file would look something like:-
<ResourceDictionary ... blah namespace stuff ...>
<Style TargetType="local:CustomControl1">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl1">
<!-- Template for Custom control 1 -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:CustomControl2">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl2">
<!-- Template for Custom control 2 -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- and so on -->
</ResourceDictionary>
The Silverlight Toolkit chapies do have a neat tool which allows you to place each control template in its own file. The tool dynamically constructs the generic.xaml from the contents of all these files. I really wish they'd blog about it so we could find out how to use it ourselves. Hello any of you Msofties lurky listening in?

Resources