Wpf - Using Shapes as a Resource - wpf

Iv'e got a Rectangle in my resource dictionary which i would like to place inside numerous Grid objects
<Rectangle HorizontalAlignment="Left" Width="10" x:Key="ShadowRect">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
Now of course i could place it directly like so :
<Grid>
<Rectangle HorizontalAlignment="Left" Width="10" >
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
but i would like to use it as a Resource so i do not have to write this XAML for every Grid ,
how can i place the rectangle using it's resource key ?

You could have a Rectangle Style instead of a Rectangle in your ResourceDictionary:
<Style x:Key="ShadowRectStyle" TargetType="Rectangle">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Width" Value="10"/>
<Setter Property="Fill">
<Setter.Value>
<LinearGradientBrush EndPoint="1.7,0.603" StartPoint="0.3,0.603">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Black" Offset="0.009"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
Then use it like this:
<Grid>
<Rectangle Style="{StaticResource ShadowRectStyle}"/>
</Grid>

Related

WPF radio button icon change (the circle itself)

I would like to have a group of radio buttons which the circles for un-selected and selected mode are changed to circle icons that I designed.
It that possible to do that in WPF?
Thanks in advance
Create a style and override the default template for RadioButtons. Something like this:
<Window.Resources>
<Style TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Grid Width="13" Height="13">
<Ellipse x:Name="Border" StrokeThickness="2">
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Green" Offset="0" />
<GradientStop Color="Pink" Offset="1" />
</LinearGradientBrush>
</Ellipse.Stroke>
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Orange" />
<GradientStop Color="Red"
Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse x:Name="CheckMark"
Margin="4"
Visibility="Collapsed">
<Ellipse.Fill>
<SolidColorBrush Color="Purple" />
</Ellipse.Fill>
</Ellipse>
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter Margin="4,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True" />
</BulletDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Yes, of course, it is possible. You can override default ControlTemplate and create style for your own radiobutton. Here is an example, you can also use Style Snooper to see the WPF built-in radio button style (a big piece of XAML code:) )

Clicking between columns to fix the size, eliminates my size settings - the WPF DataGrid

I defined in the DataGrid the size of the columns in the following way:
<DataGrid x:Name="DG" ItemsSource="{Binding X}" AutoGenerateColumns="False" ColumnWidth="*">
This works fine until the moment I press between the two columns to see the entire contents of a particular column, then he does not know my definition of this column and increases the column more than necessary by its contents.
Why is this happening and How can prevent it?
You can prevent this by using style from this site: http://msdn.microsoft.com/en-us/library/ff506248.aspx and remove from that: PART_LeftHeaderGripper and PART_RightHeaderGripper.
<Window.Resources>
...
<!--Style and template for the DataGridColumnHeader.-->
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Border x:Name="columnHeaderBorder"
BorderThickness="1"
Padding="3,0,3,0">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="{DynamicResource BorderLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource BorderDarkColor}"
Offset="1" />
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="{DynamicResource ControlLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource ControlMediumColor}"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<!--<Thumb x:Name="PART_LeftHeaderGripper"
HorizontalAlignment="Left"
Style="{StaticResource ColumnHeaderGripperStyle}" />
<Thumb x:Name="PART_RightHeaderGripper"
HorizontalAlignment="Right"
Style="{StaticResource ColumnHeaderGripperStyle}" />-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="{DynamicResource ControlLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource ControlMediumColor}"
Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
...
</Window.Resources>

How to create a bevelled gradient border in WPF

I'm trying to create a rounded end progress bar with a beveled border. I've got the progress bar looking like what I want but I'm having difficulty with making the border seemed beveled.
Can anyone help me out with this please?
An image of what I'm trying to get it to look like is here! Bevelled border
My current XAML code for the Window is as follows:
<Window x:Class="SplashDemo2.ProgressBarWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ProgressBarWindow" Height="100" Width="500">
<Window.Resources>
<Style x:Key="ProgressBarStyle" TargetType="ProgressBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressBar">
<Border BorderBrush="#1D4276" BorderThickness="5"
CornerRadius="15" Padding="0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,0.9" StartPoint="0.5,0">
<GradientStop Color="#FFEAEAEA" Offset="0.9"/>
<GradientStop Color="#FF646464"/>
</LinearGradientBrush>
</Border.Background>
<Grid x:Name="PART_Track" >
<Rectangle x:Name="PART_Indicator"
HorizontalAlignment="Left"
RadiusX="10" RadiusY="10">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,0.9" StartPoint="0.5,0">
<GradientStop Color="#FF226494" Offset="0.9"/>
<GradientStop Color="#FFEBEFFA"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ProgressBar Value="50" Width="380" Height="25"
Style="{StaticResource ProgressBarStyle}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Window>
Can anyone help me to get the border looking like the image? Many thanks.
It looks to me as though the main thing you're missing is the gradient brush on the border itself.
If you omit BorderBrush="#1D4276" and instead include something like the below, you'll be a lot closer:
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,0.9" StartPoint="0.5,0">
<GradientStop Color="#FFEBEFFA" Offset="0.9"/>
<GradientStop Color="#FF226494"/>
</LinearGradientBrush>
</Border.BorderBrush>

silverlight styles

<navigation:Page.Resources>
<Style x:Key="PageBackground" TargetType="Grid">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="Silver"/>
</LinearGradientBrush>
<Path x:Name="shinePath" Data="M0,0 L0,300 C-5.5,306.5 40,68 215,0 z" Stretch="Fill" Opacity="0.1">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Pad">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#00FFFFFF" Offset="0.871"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
</Setter.Value>
</Setter>
</Style>
</navigation:Page.Resources>
error- property 'Value' is set more then once
You've got a single <Setter.Value> element, it can contain only one child elment. Looks to me as though the Path is intended for a different property. Can't think what though Grid doesn't have a property that can accept a path. Do you intend the path to be the content of the Grid?

Vista style window Close button in XP

How can I create a a custom button that looks and feels like Vista close button? Can any one have a template?
This template will helps to make a Vista style Window close button
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<Rectangle Width='60' Height='40' x:Name='MyRectangle'>
<Rectangle.Fill>
<LinearGradientBrush x:Key="RedButtonBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#F89C8C" />
<GradientStop Offset="0.45" Color="#D47F75" />
<GradientStop Offset="0.45" Color="#C04C3C" />
<GradientStop Offset="1" Color="#C98172" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName='MyRectangle' Property="Fill" >
<Setter.Value>
<LinearGradientBrush x:Key="RedButtonMouseOverBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#F89C8C" />
<GradientStop Offset="0.45" Color="#E36A53" />
<GradientStop Offset="0.45" Color="#C72B0E" />
<GradientStop Offset="0.75" Color="#D44310" />
<GradientStop Offset="1" Color="#F5E478" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>

Resources