BorderThickness doesn't work - wpf

I'm trying to create style for button in XAML, here is my code:
<Window.Resources>
<Style x:Key="buttons"
TargetType="Control">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<GradientStop Color="GoldenRod"
Offset="0" />
<GradientStop Color="Gold"
Offset="0.10" />
<GradientStop Color="White"
Offset="0.45" />
<GradientStop Color="Gold"
Offset="0.9" />
<GradientStop Color="GoldenRod"
Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontFamily"
Value="Consolas" />
<Setter Property="FontSize"
Value="15" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="BorderThickness"
Value="5" />
<Setter Property="Padding"
Value="0,0" />
</Style>
</Window.Resources>
Everything works fine except BorderThickness property - no matter what value I'm putting there, it doesn't change. I'm wondering what is missing in my code.

Checkout the Button control default template here.
http://msdn.microsoft.com/en-us/library/ms753328%28v=vs.90%29.aspx
If we see BorderThickness property has been set to fixed value 1. Hence no changes are reflected.
You need to create a new ControlTemplate for this purpose.

Related

How to change the Property="Style" in trigger

Trigger is not working. IsMouseOver is not changing style of the Button. I am not sure what is wrong with my code. I am trying to change the style property of the button on mouse over property. It keeps throwing error.
Basically I am trying to use different style on mouse over event
<Style x:Key="FancyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="{Binding FontSize}"/>
<Setter Property="Foreground" Value="{Binding Foreground}"/>
<Setter Property="FontWeight" Value="{Binding FontWeight}"/>
<Setter Property="Width" Value="{Binding Width}"/>
<Setter Property="Height" Value="{Binding Height}"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0.0" Color="White" />
<GradientStop Offset="0.2" Color="DarkGray" />
<GradientStop Offset="0.6" Color="Black" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Style" Value="{StaticResource ResourceKey=FancyButtonInvertStyle}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FancyButtonInvertStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="{Binding FontSize}"/>
<Setter Property="Foreground" Value="{Binding Foreground}"/>
<Setter Property="FontWeight" Value="{Binding FontWeight}"/>
<Setter Property="Width" Value="{Binding Width}"/>
<Setter Property="Height" Value="{Binding Height}"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0.0" Color="Black" />
<GradientStop Offset="0.4" Color="DarkGray" />
<GradientStop Offset="0.6" Color="White" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
Instead of trying to change the Style in the Trigger, change the Background directly in it. Keeps you also from repeating the Style completely over again.
<Style x:Key="FancyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="{Binding FontSize}"/>
<Setter Property="Foreground" Value="{Binding Foreground}"/>
<Setter Property="FontWeight" Value="{Binding FontWeight}"/>
<Setter Property="Width" Value="{Binding Width}"/>
<Setter Property="Height" Value="{Binding Height}"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0.0" Color="White" />
<GradientStop Offset="0.2" Color="DarkGray" />
<GradientStop Offset="0.6" Color="Black" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0.0" Color="Black" />
<GradientStop Offset="0.4" Color="DarkGray" />
<GradientStop Offset="0.6" Color="White" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
I'm not sure if you even can change the Style of itself in the Trigger the way you intend to, because it is not set via a Setter Tag in the target control itself.

Applying same style to multiple elements

I am new to using WPF and I was trying to apply Style (e.g. Background for TextBox, Button and MenuItem should be Orange). To achieve this I did something like:
<Style TargetType="TextBox" x:Key="sampleTextBox">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
and repeated the same piece of code for targettype Button and for target menu.
This is working absolutely fine. But I would like to minimize the amount of repeated code by probably having multiple targettype values.
Please let me know if it is possible.
Thanks.
<Window.Resources>
<Style x:Key="sampleTextBox">
<Setter Property="Control.FontFamily" Value="Verdana"/>
<Setter Property="Control.FontSize" Value="11px"/>
<Setter Property="Control.FontWeight" Value="Bold"/>
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Text="This is a string and it should be wrapped." Style="{StaticResource sampleTextBox}"/>
<TextBox Text="This is a string and it should be wrapped." Style="{StaticResource sampleTextBox}"/>
</StackPanel>
Style has an attribute BasedOn. http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx With this you can use Style inheritance. Define a base style with common attributes and derive your child styles with specific attributes.
You could use FrameworkElement as a TargetType:
<Style TargetType="FrameworkElement" x:Key="CommonStyle">
<Setter Property="Control.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
And then use specific styles for each element by inheriting (BasedOn) CommonStyle:
<Style TergetType="TextBox" BasedOn="{StaticResource CommonStyle}" x:Key="TextBoxStyle">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>

DataGrid header sort direction icon

How can i display custom sort direction image in wpf datagrid header?
I use this style in my datagrid. How can I add images for ascending and descending sort directions?
<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" >
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#fbfdfc" Offset="0.1" />
<GradientStop Color="#d4d5d9" Offset="0.9" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Padding" Value="3"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="SortDirection" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#ffd8a8" Offset="0.0" />
<GradientStop Color="#ffad41" Offset="0.5" />
<GradientStop Color="#fedf78" Offset="0.9" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Black" />
</MultiTrigger>
</Style.Triggers>
</Style>
If you want to change the triangles you need to override the Template, you can trigger on the SortDirection and display a different image accordingly. (Get the default templates from MSDN (Default WPF Themes link))

Why does applying this gradient style break my silverlight app?

I am having some issues with applying a gradient to a RadButton.
I have a gradient definition in my styles resource dictionairy like so :
<LinearGradientBrush x:Key="GridView_HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5B5B5B" Offset="1"/>
<GradientStop Color="#FF868686"/>
<GradientStop Color="#FF4F4F4F" Offset="0.42"/>
<GradientStop Color="#FF0E0E0E" Offset="0.43"/>
</LinearGradientBrush>
When i apply this gradient directly to the background of a RadButton everything works.
Here is the button and the template definition:
Button
<telerik:RadButton Margin="5,10,5,0" Click="RadButton_Click" Tag="30" Content="30 Days" Style="{StaticResource SliderButton}" Background="{StaticResource GridView_HeaderBackground}" />
Template:
<!-- Style Template for Slider RadButton -->
<Style x:Key="SliderButton" TargetType="telerik:RadButton">
<Setter Property="Height" Value="30" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="5,2" />
</Style>
However when applying this gradient in the resource dictionary, my application will not load it simply gets to the silverlight loading screen and then never proceeds
Here is the button and template which breaks my app.
Button:
<telerik:RadButton Margin="5,10,5,0" Click="RadButton_Click" Tag="30" Content="30 Days" Style="{StaticResource SliderButton}" />
Template:
<!-- Style Template for Slider RadButton -->
<Style x:Key="SliderButton" TargetType="telerik:RadButton">
<Setter Property="Background" Value="{StaticResource GridView_HeaderBackground}" />
<Setter Property="Height" Value="30" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="5,2" />
</Style>
When i observe the js error console in google chrome i notice the following error is produced:
"Cannot find a resource with the
name/key ResourceWrapper"
The "GridView_HeaderBackground" needs to be defined before "SliderButton". If they are in the same Xaml then that is determined by document order.

In XAML style, how to change solid background to gradient?

I've got a MainResources.xaml file in which I have a style that defines how each of my windows in my application look:
<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="5" />
<Setter Property="SnapsToDevicePixels" Value="True" />
</Style>
Instead of "WhiteSmoke" I want my background to be this gradient:
<LinearGradientBrush>
<GradientStop Color="#ccc" Offset="0"/>
<GradientStop Color="#bbb" Offset="1"/>
</LinearGradientBrush>
But the following attempt causes VS2008 to tell me "Style setters do not support child elements":
<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background">
<LinearGradientBrush>
<GradientStop Color="#ccc" Offset="0"/>
<GradientStop Color="#bbb" Offset="1"/>
</LinearGradientBrush>
</Setter>
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="5" />
<Setter Property="SnapsToDevicePixels" Value="True" />
</Style>
What is the correct way to put a gradient color as the background for this style?
You are missing the <Setter.Value>
<Style ...>
<Setter Property="...">
<Setter.Value>
<LinearGradientBrush />
</Setter.Value>
</Setter>
</Style>

Resources