How do I add this ellipse template to my button border? - wpf

I'm aware there are probably similar questions to this one but I have no idea how to sort this out. I'm rather new to XAML and have written the following:
<Window.Resources>
<ControlTemplate x:Key="ButtonTemplate">
<Ellipse Height="300" StrokeThickness="2" Width="300" >
<Ellipse.Fill>
<SolidColorBrush Color="white" Opacity="0"/>
</Ellipse.Fill>
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Orange" Offset="0.35"/>
<GradientStop Color="SeaGreen" Offset="0.7"/>
<GradientStop Color="DarkBlue" Offset="0.95"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
</ControlTemplate>
</Window.Resources>
<Grid>
<Button Height="320" Width="320" Margin="200,100,200,100" Background="White" BorderThickness="0">
<Button.Foreground>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Crimson" Offset="0.1" />
<GradientStop Color="SeaGreen" Offset="0.9" />
</LinearGradientBrush>
</Button.Foreground>
<Button.Content>
<TextBlock FontSize="30">TEST BUTTON</TextBlock>
</Button.Content>
</Button>
</Grid>
Putting the circle in a template seems to be the universally agreed way to start going about getting a circular border on the button, but nothing seems to work from there for me. I've left the template disconnected from the button here because it doesn't work, rather it seems to just overlay a solid circle on top of what is otherwise a working button. Can someone help me out?

You should specify the TargetType and add a ContentPresenter to the ControlTemplate:
<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
<Grid>
<Ellipse Height="300" StrokeThickness="2" Width="300" >
<Ellipse.Fill>
<SolidColorBrush Color="White" Opacity="0"/>
</Ellipse.Fill>
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Orange" Offset="0.35"/>
<GradientStop Color="SeaGreen" Offset="0.7"/>
<GradientStop Color="DarkBlue" Offset="0.95"/>
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
<ContentPresenter Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
And don't forget to apply the custom template by setting the Template property of the Button:
<Button Height="320" Width="320" Background="White" BorderThickness="0"
Template="{StaticResource ButtonTemplate}">
<Button.Foreground>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Crimson" Offset="0.1" />
<GradientStop Color="SeaGreen" Offset="0.9" />
</LinearGradientBrush>
</Button.Foreground>
<Button.Content>
<TextBlock FontSize="30">TEST BUTTON</TextBlock>
</Button.Content>
</Button>

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:) )

WPF Button pixelated on one site

Every button with the following style is pixelated at the right site. You can see it clearly in the selected (blue) line:
This is the part of my code which causes the problem:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border>
<Border.Background>
<VisualBrush>
<VisualBrush.Visual>
<Grid Width="80" Height="20">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="2" RadiusX="13" RadiusY="13">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="LightGray" Offset="0"/>
<GradientStop Color="Gray" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Margin="3,2" RadiusX="8" RadiusY="12">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#dfff" Offset="0"/>
<GradientStop Color="#0fff" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
<ContentPresenter
x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
Does anyone have an idea why the buttons are pixelated on the right site?
I don't think the issue is in your Button style. Here's what your style looks like in my project:
Your buttons might be inheriting style from other templates? (rectangle, border, etc)

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>

Silverlight StackPanel inside Border with rounded corners

How is this normally handled? I have this xaml:
<Border Grid.Column="1" HorizontalAlignment="Right"
VerticalAlignment="Top" Margin="10,25,10,0" Opacity="0.7"
BorderBrush="Black" BorderThickness="1" CornerRadius="5">
<StackPanel>
<StackPanel.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="LightGray" Offset="0.0" />
<GradientStop Color="Gray" Offset="1.0" />
</LinearGradientBrush>
</StackPanel.Background>
The top left and right corners of the StackPanel are LightGray, and appear to be on top of the Border, so that in the middle of the rounded Black corners is a LightGray pixel or two, breaking up the rounding. The bottom border is Gray instead of Black.
What I thought I would get with the code above is a StackPanel with rounded corners...
I would suggest putting the background on the Border and not the Stackpanel
<Border Grid.Column="1" HorizontalAlignment="Right"
VerticalAlignment="Top" Margin="10,25,10,0" Opacity="0.7"
BorderBrush="Black" BorderThickness="1" CornerRadius="5">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="LightGray" Offset="0.0" />
<GradientStop Color="Gray" Offset="1.0" />
</LinearGradientBrush>
</Border.Background>
<StackPanel>
<!-- Items here -->
</StackPanel>
</Border>

Custom grid style in Silverlight 4

I want to set background of a grid using a style. I style I'm setting the Background Property of the grid.
But I have a border filled with LinearGradientFill and a Path which also has LinearGradientFill in it.
But I'm not able to combine both.
Below is sample code. I want to create it as a style.
<Grid>
<Border BorderBrush="Black" BorderThickness="2">
<Border.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0.953" />
<GradientStop Color="White" Offset="0" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
<Path.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="White" Offset="0.779" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Grid>
It gives me an error as:
The Property 'Value' is set more than once.
Archie,
You need to use a Template in order to place arbitrary XAML into a Style. Unfortuately, only Controls have Templates, and Grids and Borders are not Controls. But there is a solution. Although not as clean as you would like, the following XAMl should accomplish your goal. You paste the following XAML into Charles Petzold's XAML Cruncher to see the results:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="400">
<UserControl.Resources>
<!-- A ContentControl template that defines your background -->
<ControlTemplate x:Key="BackgroundTemplate" TargetType="ContentControl">
<Grid>
<Border BorderBrush="Black" BorderThickness="2">
<Border.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0.953" />
<GradientStop Color="White" Offset="0" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
<Path.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="White" Offset="0.779" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Grid>
</ControlTemplate>
<!-- A ContentControl Style that references the background template -->
<Style x:Key="BackgroundStyle" TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource BackgroundTemplate}" />
</Style>
</UserControl.Resources>
<!-- Typical usage; place the background ContentControl behind your body content -->
<Grid x:Name="LayoutRoot">
<ContentControl Style="{StaticResource BackgroundStyle}" />
<TextBlock Text="Your Content" Foreground="Red" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</UserControl>

Resources