I have a template written in xaml. Can you write how a template is turned on for TextBox?
<Grid.Resources>
<Storyboard x:Key="FlashErrorIcon">
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}"/>
<DiscreteObjectKeyFrame KeyTime="00:00:03.2000000" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="00:00:01" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True"
ToolTip="{Binding ElementName=controlWithError,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<Ellipse DockPanel.Dock="Right"
ToolTip="{Binding ElementName=controlWithError,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
Width="15" Height="15"
Margin="-25,0,0,0"
StrokeThickness="1" Fill="IndianRed" >
<Ellipse.Stroke>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFFA0404" Offset="0"/>
<GradientStop Color="#FFC9C7C7" Offset="1"/>
</LinearGradientBrush>
</Ellipse.Stroke>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<TextBlock DockPanel.Dock="Right"
ToolTip="{Binding ElementName=controlWithError,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
Foreground="White"
FontSize="10"
Margin="-15,5,0,0" FontWeight="Bold">!
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder Name="controlWithError"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
I tried to include in TextBox different ways, but I didn't manage to do it. How to include this template?
<TextBox>?????????What should I write here???????????>
?????????What should I write here???????????
</TextBox>
How to turn on the template? Any help will be appreciated!
You forgot to add the key textBoxInError to your TextBox style:
<Grid.Resources>
...
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
...
</Style>
...
</Grid.Resources>
Without that key the Style is handled as default style for TextBox. In that case you would not explicitly reference a Style in your TextBox declaration, and would have to remove the Style="{StaticResource textBoxInError}" part.
EDIT: If your Style is contained in a resource dictionary (like Grid.Resources in your XAML) and has a Key as shown above (textBoxInError), you would use that Style like this:
<Grid>
<Grid.Resources>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
...
</Style>
...
</Grid.Resources>
...
<TextBox Style="{StaticResource textBoxInError}" ... />
</Grid>
You style has the following deceleration:
This means this style will be applied to all textboxs. Unless you set a different style yourself.
When you write this:
<TextBox Validation.ErrorTemplate="{StaticResource FlashErrorIcon}"
Style="{StaticResource textBoxInError}" TabIndex="1" Margin="147,145,168,131">
You're changing the default style (to the one named textboxInError ) ...
So just remove the Style attribute from the textbox.
Another Solution
If you want to give the style a specific name and not make it the default style, use:
<Style TargetType="{x:Type TextBox}" x:Key="textBoxInError" >
And then your original code will work correctly
e.g.
<TextBox Style="{StaticResource textBoxInError}" />
Related
I am trying to close a ComboBox DropDown list with a button.click event using purely XAML.
I have used a default ComboBox Template and the only major change I have made is on the Popup where under ItemsPresenter I have inserted a button like this:
<Button Grid.Row="1" Width="200" Margin="5" Content="Close ComboBox DropDown">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}" Storyboard.TargetProperty="IsDropDownOpen">
<DiscreteObjectKeyFrame KeyTime="0" Value="False" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
When I run it I get the nasty: Cannot animate the 'IsDropDownOpen' property on a 'System.Windows.Controls.ComboBox' using a 'System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames'.
Please help.
Here is the top section of ComboBox Style:
<Style TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0F0" Offset="0" />
<GradientStop Color="#FFE5E5E5" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFACACAC" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Padding" Value="6,3,5,3" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="templateRoot" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0" />
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
<Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
<Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
<ScrollViewer x:Name="DropDownScrollViewer">
<Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="33" />
</Grid.RowDefinitions>
<Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}" />
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Button Grid.Row="1" Width="200" Margin="5" Content="Close ComboBox DropDown">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}" Storyboard.TargetProperty="IsDropDownOpen">
<DiscreteObjectKeyFrame KeyTime="0" Value="False" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</ScrollViewer>
</Border>
</Themes:SystemDropShadowChrome>
</Popup>
Hello Gys,
I found this which seems to work except that after clossing the DropDown using the button, the ComboBox cannot open again.
Any ideas?
Here is the solution so far:
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}" Storyboard.TargetProperty="(ComboBox.IsDropDownOpen)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
I figured out how to do it and in the end the solution was much simpler than I initially was going about it. The issue is that I wanted to place a little close button inside the ComboBox Popup to close the Popup manually. I must admit some of the answers I got from googling contributed to my confusion.
The trick lies in understanding how exactly a CombBox works. There are three main “switches” that control the whole opening and closing of the Popup that we see in a ComboBox.
It all begins with a ToggleButton which has two states (Boolean): It is either Open or Closed. In Other words it is either ON or OFF. The ToggleButton’s ON or OFF is bound to the ComboBox’s IsDropDownOPen states of also being either YES or NO (On or OFF). In turn The ComboBox’s ON or OFF is further bound to the Popup states of also ON or OFF. These three as it were work in harmony. Consider these as being three Traffic Lights which are synchronized in such a way that when one is ON all the three must be ON and vice versa.
With this understanding, I thought to myself that if I place another ToggleButton inside the Popup that I can use as a Open/Close button which is also bound to the ComboBox’s IsDropDownOPen states of YES or NO, I could then achieve what I wanted. Exactly this is what happened.
Hence when I click the ToggleButton to start the opening process of the ComboBox:
Its IsChecked becomes YES
Which makes the CombBox’s IsDropDownOpen to change to YES
Which makes the Popup’ IsOpen to become YES
Which makes the second ToggleButton’s IsChecked state to also
become YES
When I reverse the process by clicking the ToggleButton placed on the Popup (my Close button) everything else changes their state (become NO) and the Popup closes.
Did I do the right thing or I am missing something here?
<Style x:Key="tests" TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ToggleButton
Background="{Binding Background,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay}"
IsChecked="{Binding IsChecked,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay}"
Content="{Binding Content,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I am using above code to customize toggle button functionality, but i want to show the toggle button as rounded corner.
To get a toggle button with rounded corners you can do the following :
Right click on the button and select Edit Template > Create a copy
Give your style a name
Search for the Border template in the resource code and set the corner radius to 33 (CornerRadius="33")
Hope this helps..!
In recent versions of UWP, ToggleButton exposes a CornerRadius property (thanks #Clint Rutkas).
Otherwise:
<ToggleButton
Background="Transparent"
FontSize="16"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsTabStop="False"
ToolTipService.ToolTip="Press to enable/disable metronome (Space)">
<ToggleButton.KeyboardAccelerators>
<KeyboardAccelerator Key="Space" />
<KeyboardAccelerator Key="P" />
</ToggleButton.KeyboardAccelerators>
<ToggleButton.Resources>
<x:String x:Key="Play"></x:String>
<x:String x:Key="Stop"></x:String>
<Style TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Border
x:Name="border"
Width="56"
Height="56"
Padding="2"
Background="{TemplateBinding Background}"
BorderBrush="{ThemeResource ButtonBorderThemeBrush}"
BorderThickness="1"
CornerRadius="28">
<FontIcon x:Name="icon" Glyph="{StaticResource Play}" />
</Border>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<StateTrigger IsActive="{Binding IsEnabled}" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="icon.Glyph" Value="{StaticResource Stop}" />
<Setter Target="icon.FontSize" Value="30" />
<Setter
Target="icon.Foreground"
Value="{ThemeResource SystemColorHighlightTextColor}" />
<Setter
Target="border.Background"
Value="{ThemeResource SystemColorHighlightColor}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Resources>
</ToggleButton>
i am using pivot control, and i want to change the forground color of headers!
but somehow i am not able to do it with pretty easy guess !
<Pivot x:Name="pivot1">
<PivotItem x:Name="pivot1item1" Header="Profile" Style="{StaticResource PuzzlePivotItemHeader}">
<Controls:Profile />
</PivotItem>
<PivotItem x:Name="pivot1item2" Header="Filters" Style="{StaticResource PuzzlePivotItemHeader}">
<Controls:Filters />
</PivotItem>
</Pivot>
and style is :
<Style x:Key="PuzzlePivotItemHeader" TargetType="PivotItem">
<Setter Property="Foreground" Value="White"/>
</Style>
i just want the header fontsize change and color as white !!
how could it possible?
Here's how you'd change the foreground color and font size of the pivot item headers for all pivot controls in your app (I'm not sure how to do it per pivot control It turns out it is a bug; see this thread):
In App.xaml override these resources:
<Application.Resources>
<SolidColorBrush x:Key="PivotHeaderForegroundSelectedBrush" Color="Red" />
<SolidColorBrush x:Key="PivotHeaderForegroundUnselectedBrush" Color="Cyan" />
<x:Double x:Key="PivotHeaderItemFontSize">40</x:Double>
</Application.Resources>
If you don't care about having different colors for the selected and unselected pivot items, you can style the headers on a per-pivot basis like this instead:
<Pivot>
<Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Cyan" FontSize="40" />
</DataTemplate>
</Pivot.HeaderTemplate>
<PivotItem Header="one" />
<PivotItem Header="two" />
<PivotItem Header="three" />
<PivotItem Header="four" />
<PivotItem Header="five" />
<PivotItem Header="six" />
</Pivot>
If you just want to change the background color of all the headers, this is how you can do it in Window Phone 8.1.
First, use Expression Blend to generate the default style of the Pivot control.
<Thickness x:Key="PivotPortraitThemePadding">19,38,0,0</Thickness>
<Thickness x:Key="PivotLandscapeThemePadding">19,25,0,0</Thickness>
<Style x:Key="CustomPivotStyle" TargetType="Pivot">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Pivot">
<Grid x:Name="RootElement" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Orientation">
<VisualState x:Name="Portrait">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPortraitThemePadding}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Landscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotLandscapeThemePadding}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="TitleContentControl" ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Style="{StaticResource PivotTitleContentControlStyle}"/>
<ScrollViewer x:Name="ScrollViewer" HorizontalSnapPointsAlignment="Center" HorizontalSnapPointsType="MandatorySingle" HorizontalScrollBarVisibility="Hidden" Margin="{TemplateBinding Padding}" Grid.Row="1" Template="{StaticResource ScrollViewerScrollBarlessTemplate}" VerticalSnapPointsType="None" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled" VerticalContentAlignment="Stretch" ZoomMode="Disabled">
<PivotPanel x:Name="Panel" VerticalAlignment="Stretch">
<PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">
<PivotHeaderPanel.RenderTransform>
<CompositeTransform x:Name="HeaderTranslateTransform" TranslateX="0"/>
</PivotHeaderPanel.RenderTransform>
</PivotHeaderPanel>
<ItemsPresenter x:Name="PivotItemPresenter">
<ItemsPresenter.RenderTransform>
<TranslateTransform x:Name="ItemsPresenterTranslateTransform" X="0"/>
</ItemsPresenter.RenderTransform>
</ItemsPresenter>
</PivotPanel>
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Find this line below, the only change I have made to the default style is adding Background="{TemplateBinding BorderBrush}" to the PivotHeaderPanel which is the panel that hosts all the headers.
<PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">
The reason that I use TemplateBinding here is because doing this gives me the flexibility to change the headers background by specifying the BorderBush of the Pivot. As the BorderBrush is not used anywhere in the control, there won't be any side effect if we change it.
So, all you need to do in your Pivot is this.
<Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True" Style="{StaticResource CustomPivotStyle}" BorderBrush="{StaticResource PhoneAccentBrush}">
There are very helpful tools out there to debug WPF applications in run-time like Snoop, WPF Inspector and Xaml Spy which allow you to sneak peek into running application and monitor property values, DataContext changes, routing events and even triggers switching.
But what I did not managed to find in any of them is the way how to monitor VisualStateManager (VSM) transitions between states on particular selected control in run-time.
Is there any tool that could help with figuring out why a control doesn't go into the particular expected state (considering absence of source code for this application/library)?
Update: The question is mostly about debugging compiled code, when you have no access to the source. For cases when you have access to source code here is pretty good explanation of one of the ways to go.
There are some reasons why a control does not transect its states.
There might be more than one VisualStateGroup and its has been named as something.
Calling a visual state before a control is loaded.
VisualState will be applied only after the control's OnApplyTemplate call.
if there are any syntax error.
How ever the Visual states are not able to tract with something but it can be debugged with the return value of GoToState method.
Hope this would helpful..
I has this issue because I added a "<ControlTemplate.Triggers>" section, so I had both "Style.Triggers" and "ControlTemplate.Triggers", it worked until I click on a column header, which generated the error "ExceptionObject {"La propriété '[Unknown]' ne pointe pas vers un DependencyObject du chemin '(0).(1)[1].(2)'."} object {System.InvalidOperationException}"
<Style x:Key="GridViewColumnHeaderNoResize" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Foreground" Value="{DynamicResource SolidColorBrushW1}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewColumnHeader">
<Grid MinHeight="35">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Storyboard.TargetName="HeaderBorder">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="HeaderBorder" BorderThickness="0,1,0,1" Padding="2,0,2,0">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource BorderLightColor}" Offset="0.0" />
<GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource ColorN3}" Offset="0.0" />
<GradientStop Color="{DynamicResource ColorN3}" Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter x:Name="HeaderContent"
Margin="0,0,0,1"
RecognizesAccessKey="True"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0,0,-9,0"
Style="{StaticResource GridViewColumnHeaderGripper}" IsEnabled="False" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource SolidColorBrushN4}" TargetName="HeaderBorder" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Role" Value="Floating">
<Setter Property="Opacity" Value="0" /> <!-- 0.7 -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewColumnHeader">
<Canvas Name="PART_FloatingHeaderCanvas">
<Rectangle Fill="#60000000"
Width="{TemplateBinding ActualWidth}"
Height="{TemplateBinding ActualHeight}" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Role" Value="Padding">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewColumnHeader">
<Border Name="HeaderBorder" BorderThickness="0,1,0,1">
<Border.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Border.Background>
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource BorderLightColor}" Offset="0.0" />
<GradientStop Color="{DynamicResource BorderDarkColor}" Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource SolidColorBrushN4}" />
</Trigger>
</Style.Triggers>
</Style>
I created a style and controltemplate for my ComboBox and I would like to dress it up with an animation.
How do I create a storyboard animation for when I hover in a ComboBoxItem the highlight will fade in and once I hover out, the highlight fades out as well? Thanks!
Here is my code so far:
<!--Area which contains selected items in the ComboBox-->
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<!--THIS MUST BE NAMED AS Part_ContentHost-->
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}"/>
</ControlTemplate>
<!--ComboBox Style. Uses ComboBoxToggleButton to expand and collapse a Popup control SimpleScrollViewer to all items to be scrolled and SimpleComboBoxItem to define the look of each item. The Popup shows a list of items in a StackPanel-->
<Style TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<!--The ToggleButton is databound to the ComboBox itself to toggle IsDropDownOpen-->
<ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="True"/>
<!--The TextBox must be named PART_EditableTextBox or ComboBox will not recognize it-->
<TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" IsReadOnly="{TemplateBinding IsReadOnly}" />
<!-- The Popup shows the list of items in the ComboBox. IsOpen is databound to IsDropDownOpen which is toggled via the ComboBoxToggleButton -->
<Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
<Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
<Border x:Name="DropDownBorder" Background="{DynamicResource ComboBoxWindowBackgroundBrush}" BorderBrush="{DynamicResource ComboBoxSolidBorderBrush}" BorderThickness="1"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<!-- The StackPanel is used to display the children by setting IsItemsHost to be True -->
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<!-- This forces the DropDown to have a minimum size if it is empty -->
<Trigger Property="HasItems" Value="false">
<Setter Property="MinHeight" Value="95" TargetName="DropDownBorder"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxDisabledForegroundBrush}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="AllowsTransparency" SourceName="Popup" Value="true">
<Setter Property="CornerRadius" Value="4" TargetName="DropDownBorder"/>
<Setter Property="Margin" Value="0,2,0,0" TargetName="DropDownBorder"/>
</Trigger>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox"/>
<Setter Property="Visibility" Value="Hidden" TargetName="ContentSite"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--This is used for each item inside of the ComboBox. You can change the selected color of each item below-->
<Style TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Grid SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.004*"/>
<ColumnDefinition Width="0.996*"/>
</Grid.ColumnDefinitions>
<Border x:Name="BorderItem" Grid.Column="1" Grid.ColumnSpan="2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
<Border x:Name="BorderSelectedItem" Grid.Column="1" Grid.ColumnSpan="2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Path x:Name="ItemSelectedArrow" Data="M0.5,0.25 L0.5,22.25 19.5,22.25 z" Fill="#FFFFB14C" HorizontalAlignment="Left" Width="10.248" Height="10" Stretch="Fill" StrokeThickness="0" Margin="-0.376,-0.168,0,-0.332" Grid.Column="1" Visibility="Hidden">
</Path>
</Border>
<ContentPresenter x:Name="ContentSite" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="2,2,0,2"/>
</Grid>
<ControlTemplate.Triggers>
<!-- Change IsHighlighted SelectedBackgroundBrush to set the selection color for the items -->
<Trigger Property="IsSelected" Value="True">
<!--<Setter Property="Background" Value="{DynamicResource ComboBoxSelectedBackgroundBrush}" TargetName="BorderSelectedItem"/>-->
<Setter Property="Visibility" Value="Visible" TargetName="ItemSelectedArrow"/>
<Setter Property="Margin" Value="10,2,0,2" TargetName="ContentSite"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Background" Value="{DynamicResource ComboBoxHighlightBackgroundBrush}" TargetName="BorderItem"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxDisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
So with your Item Template opened in blend go to your States Tab (Or just do it directly in your VisualStateManager XAML for your MouseOver State) and for starters adjust your Time Duration to a longer time to decrease the speed of the transition and make the fade effect appear. You can also set your Transition Effect and Easing Function to provide some further coolness to your simple transition. It's easiest just using blend but here's a quick n dirty xaml example that might give a better idea. Hope it helps, good luck!
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Normal" **GeneratedDuration="0:0:0.15"** To="MouseOver">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseIn" Exponent="7"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="MouseOver" **GeneratedDuration="0:0:0.15"** To="Normal">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition GeneratedDuration="0:0:0.15"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.0500000" Value="1.0" KeySpline="0,0,0.0299999993294477,0.920000016689301"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimation Duration="0" To="{StaticResource BaseColor2}" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter" d:IsOptimized="True"/>
</Storyboard>
</VisualState>