Reputation: 707
I have a tab control that is docked to the right side of a dock panel. The tab control's width is set to 10 and opacity to 0. I would like to have the tab control's width change to 200 and opacity to 100 when I move my mouse over it. Then when I move my mouse away, have the tab control go back to its original values. I cannot figure out how to do that. Can someone help please? Below is my markup, where I was trying to get the tab control to show first.
<UserControl x:Class="Cordata.Mrs.MVVM.Views.Controls.SlideoutView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<UserControl.Resources>
<Storyboard x:Key="OnMouseEnter1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="OptionsSlideout">
<EasingDoubleKeyFrame KeyTime="0" Value="200"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
</EventTrigger>
</UserControl.Triggers>
<DockPanel HorizontalAlignment="Stretch">
<TabControl Name="OptionsSlideout" TabStripPlacement="Bottom" Width="10" Margin="0,0,0,5" HorizontalAlignment="Stretch" DockPanel.Dock="Right" Opacity="0">
<TabItem Header="Configure">
<StackPanel Orientation="Vertical">
<Button Name="ConfigurationBuilderButton" Width="80" Cursor="Hand" Click="ConfigurationBuilderButton_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel>
<Image Source="/Images/ConfigurationBuilder.png" ToolTip="Run Configuration Builder" />
<TextBlock Text="Configuration Builder" TextWrapping="Wrap" FontWeight="Bold" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
<Button Name="LoadConfigurationButton" Width="80" Cursor="Hand" Margin="0,20,0,0" Click="LoadConfigurationButton_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel>
<Image Source="/Images/LoadCustomConfiguration.png" ToolTip="Load Custom Configuration" />
<TextBlock Text="Load Configuration" TextWrapping="Wrap" FontWeight="Bold" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</TabItem>
<TabItem Header="Help">
<TextBlock Text="Help" />
</TabItem>
</TabControl>
</DockPanel>
</UserControl>
Upvotes: 4
Views: 3945
Reputation: 69959
I needed to add a Rectangle
so that the DockPanel
could have some main content and therefore allow us to dock your TabControl
to the right. You don't need any of your Resources
for this and one other thing... I had to remove your handlers because I didn't have them implemented in my test project. Anyway, this will do what you want just using a Trigger
on the IsMouseOver
property:
<DockPanel HorizontalAlignment="Stretch">
<TabControl Name="OptionsSlideout" TabStripPlacement="Bottom" Width="10" Margin="0,0,0,5" DockPanel.Dock="Right" Opacity="0.0">
<TabControl.Style>
<Style>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Trigger.EnterActions>
<StopStoryboard BeginStoryboardName="CloseStoryBoard" />
<BeginStoryboard Name="OpenStoryBoard">
<Storyboard DecelerationRatio="0.8">
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)" To="200.0" />
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1.0" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="OpenStoryBoard" />
<BeginStoryboard Name="CloseStoryBoard">
<Storyboard DecelerationRatio="0.8">
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)" To="10.0" />
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.0" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</TabControl.Style>
<TabItem Header="Configure">
<StackPanel Orientation="Vertical">
<Button Name="ConfigurationBuilderButton" Width="80" Cursor="Hand">
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel>
<Image Source="/Images/ConfigurationBuilder.png" ToolTip="Run Configuration Builder" />
<TextBlock Text="Configuration Builder" TextWrapping="Wrap" FontWeight="Bold" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
<Button Name="LoadConfigurationButton" Width="80" Cursor="Hand" Margin="0,20,0,0">
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel>
<Image Source="/Images/LoadCustomConfiguration.png" ToolTip="Load Custom Configuration" />
<TextBlock Text="Load Configuration" TextWrapping="Wrap" FontWeight="Bold" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</TabItem>
<TabItem Header="Help">
<TextBlock Text="Help" />
</TabItem>
</TabControl>
<Rectangle Name="DummyContent" Fill="White" />
</DockPanel>
For future reference Mike, this is a bit too much code to put up here. It would have been easier for users to visualize your problem if you had just used coloured Rectangle
or Border
elements and said I'd like to animate this Border
. That way, missing handlers, images, Style
and/or Resources
elements don't slow down users trying to help you. Simplifying your problem before you post is always good and sometimes, you even end up fixing your own problems. Anyway, it's not a problem and that code does what you want.
Upvotes: 5