WPF change background of disabled buttons - wpf

I am trying to learn WPF and I have made a simple Tic Tac Toe application. The application works but I'm trying to style it a bit. I can't find out how to set the button disabled background for all my buttons (and keep the content visible). My question is how set up the code in the resource dictionary? The commented code is what I tried. To give all buttons the same style I added a resource dictionary.
The resource dictionary
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!--<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Path=SelectedDate,
StringFormat={}{0:d},
RelativeSource={RelativeSource TemplatedParent}}"
VerticalAlignment="Center" HorizontalAlignment="Left"
Padding="4,0,0,0" />
</Border>-->
<!--<Border BorderBrush="Aquamarine" BorderThickness="1"></Border>-->
<!-- Outer Rectangle with rounded corners. -->
<!-- Present Content (text) of the button. -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
App.xaml
<Application x:Class="FirstApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="ResourceDictionary.xaml" />
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="FirstApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="525">
<Grid Margin="0,0,0.4,-19.2" Name="controlGrid">
<Button x:Name="A" Content="" HorizontalAlignment="Left" Margin="117,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="B" Content="" HorizontalAlignment="Left" Margin="223,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="C" Content="" HorizontalAlignment="Left" Margin="330,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="D" Content="" HorizontalAlignment="Left" Margin="117,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" RenderTransformOrigin="0.302,0.614" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="E" Content="" HorizontalAlignment="Left" Margin="223,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="F" Content="" HorizontalAlignment="Left" Margin="330,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="G" Content="" HorizontalAlignment="Left" Margin="117,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="H" Content="" HorizontalAlignment="Left" Margin="223,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<Button x:Name="I" Content="" HorizontalAlignment="Left" Margin="330,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
<ToolBar Margin="0,0,0,450.6">
<Button Name="newButton" Click="newButton_Click">New</Button>
</ToolBar>
</Grid>
</Window>

Possibly more than you were looking for:
<Style TargetType="{x:Type Button}" x:Key="ButtonBase">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="#FF959595" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="10,0" />
<Setter Property="Margin" Value="2" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Height" Value="25" />
<Setter Property="MinWidth" Value="100" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ColorAnimation To="#FFFFFFFF" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
Duration="0:0:0.07" />
<ColorAnimation To="#FFDEDEDE" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Duration="0:0:0.07" />
<ColorAnimation To="#FF959595" Storyboard.TargetName="BrBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0.07" />
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation To="#FF00B4E4" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
Duration="0:0:0.07" />
<ColorAnimation To="#FF0083C3" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Duration="0:0:0.07" />
<ColorAnimation To="#FF4C7B8F" Storyboard.TargetName="BrBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0.07" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation To="#DBEDFD" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
Duration="0:0:0.05" />
<ColorAnimation To="#C4E0FC" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Duration="0:0:0.05" />
<ColorAnimation To="#4C7B8F" Storyboard.TargetName="BrBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0.05" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimation To="#EFEFEF" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
Duration="0:0:0" />
<ColorAnimation To="#EFEFEF" Storyboard.TargetName="BgBrush"
Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Duration="0:0:0" />
<ColorAnimation To="#D9D9D9" Storyboard.TargetName="BrBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Chrome" BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true">
<Border.BorderBrush>
<SolidColorBrush x:Name="BrBrush" Color="#ACACAC" />
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush x:Name="BgBrush" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0F0" Offset="0" />
<GradientStop Color="#FFE5E5E5" Offset="1" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#FFFFFF" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Foreground" Value="#000000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Edit based on comments/feedback by King King
The above example is certainly more than the OP has requested, however, considering the context of the question and their interest in Button control State styling, I felt it advantageous to provide additional detail (albeit without explanation admittedly).
Prior to <Setter Property="Tempalte"> I am altering the basic visual style of the Button control. I should hope none of this requires explaining as it seems fairly self explanatory.
Post <Setter Property="Template"> I am altering the visual states of the Button ControlTemplate (these being Normal, MouseOver, Pressed & Disabled). This allows me to customise the look and feel of the Button whilst it is in one of these states. In this example I am using a StoryBoard so that I can animate the Buttons State transitions.
The four States are styled in a similar format (animated gradient background & animated solid border), the differences between these states being the colours and durations they employ to provide the desired effect.
After the VisualStates I alter the main content template, how I want the Button control to be presented. I provide a ContentPresenter which will be used to display the content of the Button, I bind certain characteristics of the ContentPresenter to the Button being template, allowing for these characteristics to be controlled on a per Button basis if so desired. The ContentPresenter is surrounded by a Border which defines a BrBrush (boarder brush) and a BgBrush (background brush). These are the same brushes that are referenced in the VisualStates templates and provide a default state for them to work to/from.
Finally I alter the ControlTemplate.Triggers, customising the visual style of the Button (its foreground colour in this example) based on the Property Value of the activated Trigger.

Related

How to Create Animated Tabs with Tab Indicator using WPF?

I have a Tab control in my WPF application. And this Tab control has two Tab items. And due to design purpose I underline each of the Tab items. Now I want to animate the underline of those Tab items. I just want a simple animation when I select any of this tab items the underline should be quickly move from one tab item to another. Though there a decent space between those tab items.
I implement the underline of those tab items by Text Decoration in Control Template. I use my own custom Control Template for both of the Tab items. I use a separator in on that specific space between those tab items. I give red color for the underline of those tab header of the tab items. And I also give the red color for the separator. And it's also looks like an animated Tab indicator.
I can use rectangle but I don't use it because separator is more lightweight.
I know I have to use the storyboard property to fade out the separator but I don't know how to apply it.
What I want is when I switch between those two tab items the separator should fade out from one side to another and vice versa, So that it's look like the red underline is moving from one tab item to another through the space between them
Here is the Control Template for the tab items :
<Style x:Key="TabItemGoTwo" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="#939393" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid
x:Name="Root"
ClipToBounds="true"
KeyboardNavigation.TabNavigation="Local"
RenderOptions.BitmapScalingMode="NearestNeighbor"
RenderOptions.ClearTypeHint="Enabled"
SnapsToDevicePixels="true"
UseLayoutRounding="True">
<Border
x:Name="Border"
Margin="0,0,-4,0"
BorderThickness="1,1,1,1"
CornerRadius="2,12,0,0">
<Border.BorderBrush>
<SolidColorBrush Color="#282828" />
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.0" Color="#282828" />
<GradientStop Offset="1.0" Color="#282828" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter
x:Name="ContentSite"
Margin="0,2,12,2"
HorizontalAlignment="Left"
VerticalAlignment="Center"
ContentSource="Header"
RecognizesAccessKey="True"
RenderOptions.BitmapScalingMode="NearestNeighbor"
RenderOptions.ClearTypeHint="Enabled"
SnapsToDevicePixels="True"
UseLayoutRounding="True" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Text="{TemplateBinding Content}"
UseLayoutRounding="True">
<TextBlock.TextDecorations>
<TextDecoration PenOffset="4" PenOffsetUnit="Pixel">
<TextDecoration.Pen>
<Pen Brush="#fe0000" Thickness="3" />
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#fdfdfd" />
<Setter Property="Panel.ZIndex" Value="100" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Root" Property="Background" Value="#282828" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#fdfdfd" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here is my code for the Tab items, separator within that Tab control.
<StackPanel Width="645" HorizontalAlignment="Left" Height="460" VerticalAlignment="Top" Margin="-4,59,0,0" UseLayoutRounding="True" >
<TabControl x:Name="MyTabControl" SelectionChanged="MyTabControl_SelectionChanged" BorderThickness="0" Background="#282828" Width="656" HorizontalAlignment="Left" Height="462" VerticalAlignment="Top" Margin="-5,1,-0.2,0" >
<TabItem x:Name="TabItemFirst" Style="{StaticResource TabItemGoTwo}" Header="File manager" Margin="34,0,-26.6,0" Height="24" FontSize="10" VerticalAlignment="Center" UseLayoutRounding="True" RenderOptions.ClearTypeHint="Enabled" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display" FontFamily="Segoe UI" >
<Grid Background="#222222" Height="433" HorizontalAlignment="Left" VerticalAlignment="Top" Width="645" Margin="0,5,0,0" >
<Label Name="Folder" Content="Folder:" FontSize="10" Foreground="#efefef" Height="20" Width="40" Margin="-571,-367,0,0" FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display" />
<Button Name ="FolderSelect" Width="532" HorizontalAlignment="Left" VerticalAlignment="Top" Height="33" Margin="85,17,0,0" Background="#1a1a1a"
materialDesign:ShadowAssist.ShadowDepth="Depth0" materialDesign:RippleAssist.Feedback="Transparent" BorderThickness="0" UseLayoutRounding="True"
></Button>
<Label Name="ShowFolders" Content=".." Margin="-479,59,0,0" Background="#1a1a1a" Width="168" Height="373" Foreground="#efefef" ></Label>
<Button
Width="65" HorizontalAlignment="Left" Height="25" Background="#FF403D3D" Margin="16,292,0,0"
ToolTip="Resource name: MaterialDesignRaisedButton">
<materialDesign:PackIcon Kind="PlusThick" />
</Button>
<Button
Width="65" HorizontalAlignment="Left" Height="25" Background="#FF403D3D" Margin="82,292,0,0"
ToolTip="Open output folder">
<materialDesign:PackIcon Kind="FolderUpload" />
</Button>
<Label Content="Video recordings:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="269.2,-22,0,0" Foreground="#efefef" FontSize="10" FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display" />
</Grid>
</TabItem>
<TabItem x:Name="TabItemSecond" Style="{StaticResource TabItemGoTwo}" Header="Preview" FontSize="10" Height="24" Margin="47.4,0,-39.6,0" FontFamily="Segoe UI" VerticalAlignment="Center" UseLayoutRounding="True" RenderOptions.ClearTypeHint="Enabled" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display" >
<Grid></Grid>
</TabItem>
</TabControl>
</StackPanel>
<StackPanel Height="10" Width="30" Margin="-850,-395,0,0" >
<Separator Height="2" Background="#fe0000" Margin="2,4,2.4,0" SnapsToDevicePixels="True" UseLayoutRounding="True" RenderOptions.ClearTypeHint="Enabled" RenderOptions.BitmapScalingMode="NearestNeighbor" ></Separator>
</StackPanel>
I replace your last StackPanel with Canvas and update your Separator with the below Animation code:
<Canvas>
<Separator Height="2" Width="50" Background="Red" Margin="25,82,4,0" >
<Separator.Style>
<Style TargetType="Separator">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyTabControl,Path=SelectedIndex}" Value="1">
<DataTrigger.EnterActions>
<BeginStoryboard Name="MyBeginStoryboard">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" Duration="0:0:1"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="Width" From="50" To="20" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard Name="MyBeginStoryboard2">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" From="100" To="0" Duration="0:0:1"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="Width" From="30" To="50" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Separator.Style>
</Separator>
</Canvas>

WPF: TabItem Header Styling of TabControl usin WPF and C#

I'm new to WPF,
I'm trying to style a TabControl and I'm having difficulty styling the actual TabItems:
I'm sure it's probably not that difficult, but I just can't figure it out so any help would be most appreciated!
Here's the XAML for the TabControl so far:
<Window x:Class="FunctionalFun.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:gc="clr-namespace:FunctionalFun.UI"
mc:Ignorable="d"
Title="mY Application" Height="800" Width="1080" ResizeMode="NoResize">
<StackPanel>
<Grid>
<TabControl Name="TabControl1" TabStripPlacement="Left" Margin="-6,46,-14,-453">
<TabItem>
<TabItem.Header>
<Image Height="35" Width="35" Source="check-form_logo3.png"/>
</TabItem.Header>
<TabItem.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</TabItem.Resources>
<Canvas>
</Canvas>
</TabItem>
<TabItem >
<TabItem.Header>
<Image Height="35" Width="35" Source="calendar-icon_logo1.png"/>
</TabItem.Header>
<TabItem.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</TabItem.Resources>
<Canvas>
</Canvas>
</TabItem>
<TabItem >
<TabItem.Header>
<Image Width="35" Height="35" Source="table_logo4.png"/>
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Grid>
</StackPanel>
</Window>
One option is to copy the default style of tab item, then make your changes of its border corner radius, background, etc. then give it a key, then apply your style to your actual control, like so:
<StackPanel>
<Grid>
<Grid.Resources>
<Style x:Key="CustomTabItem" TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="Root"
Background="AntiqueWhite">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource ControlPressedColor}" />
</ColorAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)"
Storyboard.TargetName="Border">
<EasingThicknessKeyFrame KeyTime="0"
Value="1,1,1,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource DisabledControlDarkColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.BorderBrush).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource DisabledBorderLightColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border"
Margin="0,0,-4,0"
BorderThickness="1,1,1,1"
CornerRadius="10">
<Border.BorderBrush>
<SolidColorBrush Color="Black" />
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource ControlLightColor}"
Offset="0.0" />
<GradientStop Color="{DynamicResource ControlMediumColor}"
Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Panel.ZIndex"
Value="100" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TabControl Name="TabControl1" TabStripPlacement="Left" Margin="-6,46,-14,-453">
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="A" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="B" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="C" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Grid>
</StackPanel>

Can't do animation

I want to change size of button from 70 till 90 when mouse is over:
<Style TargetType="Button"
x:Key="RadialButton">
<Setter Property="Width"
Value="70"></Setter>
<Setter Property="Height"
Value="85"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)"
Storyboard.TargetName="ExtEllipse">
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="90" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="70"></RowDefinition>
<RowDefinition Height="15"></RowDefinition>
</Grid.RowDefinitions>
<Ellipse Width="70"
Height="70"
Stroke="Gray"
Grid.Row="0"
Name="ExtEllipse"
Fill="{x:Null}" />
<Ellipse Width="50"
Height="50"
Stroke="Gray"
Grid.Row="0"></Ellipse>
<TextBlock Grid.Row="1"
FontSize="13"
FontWeight="Bold"
TextAlignment="Center"
Foreground="Green">
<ContentPresenter RecognizesAccessKey="True"
Content="{TemplateBinding Button.Content}" />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
using:
<Button Content="Button"
HorizontalAlignment="Left"
Margin="36,140,0,147"
Width="151"
Style="{DynamicResource RadialButton}" />
but it does not work. Nothing happened. Why and how to solve this problem?
That's because you have Storyboard, but you don't play it.
Try add trigger to play that storyboard. Something like this:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Storyboard1}" />
</Trigger.EnterActions>
</Trigger>
Btw this is result of your animation:
You have to start your Storyboard. Your EventTrigger does nothing.
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Storyboard="{StaticResource Storyboard1}" />
</EventTrigger>

Convert ListBox template into Button Template

I having hard time converting an ItemTemaplte of ListBox to a Button from the example {Surfin}Video
Viewer Demo ,
I want a button style like an Item in this ListBox . Each Item in the ListBox have
Name&Image with the style define there. I noob to Template & Styles , If some1 with more expreince can
make it easily, I appreciate . ( better to run the demo to see how the 'button'( listbox item) looks like , I need the look and feel of the button only :) ignore the 'video' )
Thanks.
<Window x:Class="Surfin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Surfin"
Background="Black" x:Name="mainWindow"
MinWidth="650" MinHeight="365" Width="650" Height="365">
<!-- Declare the MyVideos class, which belongs to the DataTemplatingLab namespace.-->
<!-- Setting the Directory to the relative path pointing to the Media folder.-->
<!-- Giving this an x:Key. Now controls in this Window can bind to the videos in the Media folder.-->
<Window.Resources>
<MyVideos xmlns="clr-namespace:DataTemplatingLab" Directory="Media" x:Key="vids" />
<DataTemplate x:Key="mainScreenTemplate">
<Border BorderBrush="LimeGreen" BorderThickness="2"
CornerRadius="3" Margin="15">
<Grid>
<!-- Background image if no video is playing. -->
<Image Source="Images\Crystal.jpg" Stretch="Fill"/>
<!-- The video -->
<!-- The Source property of the video is bound to the Source property of the current MyVideo object.-->
<MediaElement Name="mainVideo" Stretch="Fill"
Source="{Binding Path=Source}"/>
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="listBoxTemplate">
<DataTemplate.Resources>
<Style TargetType="DockPanel">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="ToolTipService.ShowDuration" Value="80000"/>
</Style>
</DataTemplate.Resources>
<DockPanel Height="70" Width="160">
<Border Margin="4,5,0,0" Height="50" Width="50">
<Image Source="Images\Preview.png" />
</Border>
<TextBlock Text="{Binding Path=VideoTitle}" VerticalAlignment="Center"
TextBlock.TextTrimming="WordEllipsis" Margin="5,5,0,5"
Foreground="White" FontSize="12" FontFamily="Comic Sans MS">
</TextBlock>
</DockPanel>
</DataTemplate>
<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
<Setter Property="Margin" Value="10,10,10,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Rectangle x:Name="GelBackground" RadiusX="9" RadiusY="9"
Opacity="1" Fill="{TemplateBinding Background}"
Stroke="#66ffffff" StrokeThickness="1" />
<Rectangle x:Name="GelShine" RadiusX="6" RadiusY="6"
Opacity="1" Margin="2,2,2,0" VerticalAlignment="top"
Stroke="transparent" Height="15">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#ccffffff" Offset="0" />
<GradientStop Color="transparent" Offset="1" />
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetName="GelBackground"
TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<ColorAnimation To="LimeGreen" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetName="GelBackground"
TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<ColorAnimation Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="RoyalBlue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<!-- 1) The ListBox and ContentControl bind to the same source. -->
<!-- 2) IsSynchronizedWithCurrentItem set to true. -->
<!-- With the above 2 conditions satisfied, once the DataTemplates are in place,
the ContentControl will display the content of the selected list item.-->
<DockPanel>
<ListBox DockPanel.Dock="Left" Width="200" BorderThickness="0"
ItemsSource="{Binding Source={StaticResource vids}}"
IsSynchronizedWithCurrentItem="True"
ItemTemplate="{StaticResource listBoxTemplate}"
Background="Transparent"/>
<ContentControl Content="{Binding Source={StaticResource vids}}"
ContentTemplate="{StaticResource mainScreenTemplate}"/>
</DockPanel>
</Window>
<Style x:Key="{x:Type Button}" TargetType="Button">
<Setter Property="Margin" Value="10,10,10,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle x:Name="GelBackground" RadiusX="9" RadiusY="9"
Opacity="1" Fill="{TemplateBinding Background}"
Stroke="#66ffffff" StrokeThickness="1" />
<Rectangle x:Name="GelShine" RadiusX="6" RadiusY="6"
Opacity="1" Margin="2,2,2,0" VerticalAlignment="top"
Stroke="transparent" Height="15">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#ccffffff" Offset="0" />
<GradientStop Color="transparent" Offset="1" />
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetName="GelBackground"
TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<ColorAnimation To="LimeGreen" Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetName="GelBackground"
TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<ColorAnimation Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="RoyalBlue" />
</Trigger>
</Style.Triggers>
</Style>
Something like this. Hope this helps.

Focus on Label, TextBlock and Border

I want to create a flat button with rounded right top and bottom corners. This button needs to have the background changed on clicked and on mouse over.
Currently my Markup looks like this:
<Border x:Name="MyButton" Height="25" Margin="0,5,0,0" CornerRadius="0 5 5 0" BorderThickness="1" BorderBrush="Gray" Style="{StaticResource myStyle1}">
<StackPanel Orientation="Horizontal" Margin="8,0,0,0">
<Image Source="image.jpg" Height="20"/>
<TextBlock Text="My Button"/> <!-- Could also be a label if needs to be. -->
</StackPanel>
</Border>
<Style x:Key="myStyle1" TargetType="{x:Type Border}">
<Setter Property="Background" Value="{StaticResource MainContentForegroundColor}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
The mouse over trigger works perfectly but i can't get the click trigger to work... i've tried IsKeyboardFocusWithin and IsFocused but it didn't work.
I think you should use ToggleButton instead of normal button -
<ToggleButton
Background="Transparent">
<ToggleButton.Template>
<ControlTemplate
TargetType="{x:Type ToggleButton}">
<Border
x:Name="MyButton"
Height="25"
Margin="0,5,0,0"
CornerRadius="0 5 5 0"
BorderThickness="1"
BorderBrush="Gray">
<StackPanel
Orientation="Horizontal"
Margin="8,0,0,0">
<Image
Source="image.jpg"
Height="20" />
<TextBlock
Text="My Button" />
<!-- Could also be a label if needs to be. -->
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger
Property="IsChecked"
Value="True">
<Setter
Property="Background"
Value="Red" />
</Trigger>
<Trigger
Property="IsMouseOver"
Value="True">
<Setter
Property="Background"
Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
You can create a Button style, then only you will IsPressed Property. See the below code using VSM.
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF760D0D"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF675A88"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="border" BorderBrush="#FF5A8876" BorderThickness="3" Background="#FFF4EDED"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button HorizontalAlignment="Left" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="180" Height="61" Content="Button"/>

Resources