Sunday, 29 September 2013

How to write a style for a control that changes width of another control?

How to write a style for a control that changes width of another control?

Is there any way to write a style for a control that changes width of
another control?
<Style x:Key="SubMenuStyle" TargetType="Label">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="LightCyan"/>
<Style.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Menu"
Storyboard.TargetProperty="Width" To="0"
Duration="0:0:.5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
This code leads to error "TargetName property cannot be set on a Style
Setter"
I know I can write codes below and it works:
<Label Name="Owners" Margin="0,1,0,0" MouseLeftButtonDown="SubMenuClicked"
Style="{StaticResource SubMenuStyle}">
<Label.Triggers>
<EventTrigger RoutedEvent="MouseLeftButtonDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Menu"
Storyboard.TargetProperty="Width" To="0"
Duration="0:0:.5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Label.Triggers>
</Label>
but since I use this trigger in multiple labels I want to write it in a
style once
this is my code to define controls:
<Border Name="Menu" Grid.Column="2" Grid.Row="1" Width="0"
HorizontalAlignment="Right" BorderBrush="LightBlue" BorderThickness="2"
CornerRadius="2" Background="LightCyan">
<StackPanel Name="MenuPanel">
<Button Style="{StaticResource MenuButtonsStyle}">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ListsMenu"
Storyboard.TargetProperty="Height"
To="86" Duration="0:0:.6"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<StackPanel Name="ListsMenu" Height="0">
<Label Name="Owners" Margin="0,1,0,0"
MouseLeftButtonDown="SubMenuClicked"
Style="{StaticResource SubMenuStyle}"/>
<Label Name="Contacts"
MouseLeftButtonDown="SubMenuClicked"
Style="{StaticResource SubMenuStyle}"/>
<Label Name="Groups"
MouseLeftButtonDown="SubMenuClicked"
Style="{StaticResource SubMenuStyle}"/>
</StackPanel>
</StackPanel>
</Border>

No comments:

Post a Comment