Silverlight 4 DataGrid column header style - silverlight

I am trying to affect the background pattern on a DataGrid in Silverlight 4. I have the following style:
<Style x:Key="DashboardGridHeaderStyle"
TargetType="primitives:DataGridColumnHeader">
<Setter Property="FontSize"
Value="14" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Foreground"
Value="{StaticResource xrxGray_I}" />
<Setter Property="Background"
Value="{StaticResource xrxGray_B}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Height="50" Width="100">
<TextBlock Text="{TemplateBinding Header}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This causes an exception when it is applied. This is caused by the Template setter. Does anyone know how to change the background of the column header (I want a solid color instead of the default gradient)?
Thanks for any help.

Your ControlTemplate element is missing the TargetType property it should like this:-
<ControlTemplate TargetType="primitives:DataGridColumnHeader">

Related

How Style applies to child controls?

I expect the following Style to affect any Border that is child of the Expander object to which this Style is applied to have a 1-pixel thick gray border. Why doesn't it work?
<Style TargetType="Expander">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border>
<Border.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="1" />
</Style>
</Border.Resources>
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

Simple working example to override default visual style of a button in wpf

I want to create custom visual style for my buttons.
I need a simple working example of how to override default visual style of a button. As well as a simple explanation of how to apply it.
I want to get something working, so I can start from there and experiment my way further.
I've tried to add a new recourse dictionary as follows:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="mstyle" TargetType="Button">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
after that I've created some new button in runtime and tried to apply this style to it:
Dim MyButton As New Button
Dim st As New Style
st = Application.Current.FindResource("mstyle")
MyButton.Style = st
When I try to run this, I get an error that the recourse 'mstyle' could not be found.
You don't in most cases need any code behind to do that all what you need is to define a custom style that target your button in the resource dictionary or in the window resource here an example :
<Style x:Key="DarkStyleButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E59400" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
first set the value for the properties you want to customize,
then set the button template and don't forget to add the
ContentPresenter that will hold the button content
finally define triggers to handle the mouse over, click and what ever
else you want to set a custom look when it triggers (for example the
desable/enabled )
To use that style here how
<Button x:Name="BrowseButton" Margin="5" Style="{StaticResource DarkStyleButton}" ToolTip="tooltip about the button">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="../BrowseImage.png"/>
<TextBlock Text="Browse" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5"></TextBlock>
</StackPanel>
</Button.Content>
</Button>

Customize WPF Toolkit autocompletebox

WPF Toolkit library has Autocompletebox control.
1. How to place an icon at the left of its Textbox part?
2. How to add shadow to the borders of the suggestions popup?
Changing TextBoxStyle property does not give any results.
<!-- xmlns:extendedControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" -->
<extendedControls:AutoCompleteBox>
<extendedControls:AutoCompleteBox.TextBoxStyle>
<Style TargetType="TextBox">
<Setter Property="Padding" Value="20,0,0,0" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/img.png" Stretch="None" AlignmentX="Left" />
</Setter.Value>
</Setter>
</Style>
</extendedControls:AutoCompleteBox.TextBoxStyle>
</extendedControls:AutoCompleteBox>
The Autocompletebox have got a property : TextBoxStyle where you can chose the style of the textbox.
Or you can do that in the property Style like that :
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="TextboxStyle2">
<Setter Property="MaxLength" Value="74" />
<Setter Property="Width" Value="300" />
</Style>
<Style TargetType="controls:AutoCompleteBox" x:Key="TextBoxStyle">
<Setter Property="TextBoxStyle" Value="{StaticResource TextboxStyle2}" />
<Setter Property="IsTextCompletionEnabled" Value="True" />
</Style>
</UserControl.Resources>
/*...*/
<controls:AutoCompleteBox Name="Titre" Style="{StaticResource TextBoxStyle}"/>
But i don't know how to add the shadow. Try the others properties. =)
Edit :
To change background, there is already some tutoriels.
how to separate the background image in wpf textbox?
Exemple :
<Style TargetType="TextBox" x:Key="TextboxStyle2">
<Setter Property="MaxLength" Value="74" />
<Setter Property="Width" Value="300" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="../../../Ressources/Icones/Find_5650.ico" Stretch="None" AlignmentX="Left" AlignmentY="Top" />
</Setter.Value>
</Setter>
</Style>

Silverlight datagrid columnheader change font color

Is it possible to change the font color for the column headers in a datagrid?
I've already applied some styling which changes the background, but i can't figure out how to change the font color, not for the element, but for the header.
the XAML I'm using is from this thread: Change background color of Datagrid Header in Silverlight
Hi add a style to your resource and set the style,
<Style x:Key="GridHeaderStyle" ControlTemplate TargetType="primitives:DataGridColumnHeader">
<Setter Property="FontSize"
Value="14" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Foreground"
Value="{StaticResource xrxGray_I}" />
<Setter Property="Background"
Value="{StaticResource xrxGray_B}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Height="50" Width="100">
<TextBlock Text="{TemplateBinding Header}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>

Adding a ContentPresenter to the style of a Control

I'm trying to style an element in WPF which displays a yellow border around anything thats in it and shows a tool-tip when the cursor is over it. The problem is that I don't have much of an idea how to do this and anything I tried does not seem to work.
Here is what I have until now:
<Style x:Key="HistoryElementStyle"
TargetType="{x:Type Control}">
<Setter Property="BorderBrush"
Value="Yellow"/>
<Setter Property="BorderThickness"
Value="1.5" />
<Setter Property="CornerRadius"
Value="2" />
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip Template="{StaticResource HistoryTooltipTemplate}" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ContentPresenter x:Name="PART_Content"
Width="Auto"
HorizontalAlignment="Stretch"
ContentSource="Content"
IsEnabled="{TemplateBinding IsEnabled}" />
</Setter.Value>
</Setter>
</Style>
Visual Studio complains that the ContentPresenter is an invalid type.
Cheers
AC
You must wrap the ContentPresenter around a <ControlTemplate TargetType="Control" /> to match the type of the Control.Template property.
I'd try adding the DataTempalte tag around the ContentPresenter (sorry, I cannot test from where I am writing this).

Resources