locked
StackPanel Orientation Binding to a ToggleButton? RRS feed

  • Question

  • Hi,

    is it possible to bind the Orientation of a StackPanel to a ToggleButton? ToggleButton is Ckhecked - the Orientation of the Stackpanel is horizontal // ToggleButton is Unchecked - the Orientation is vertikal

    Can you help me? Thanks a lot.

    Friday, July 1, 2011 9:24 AM

Answers

  • Yeah, just use triggers + storyboards:

    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="TestBed.Window1"
    	x:Name="Window"
    	Title="Window1"
    	Width="640" Height="480">
    	<Window.Resources>
    		<Storyboard x:Key="OnChecked1">
    			<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="stackPanel" Storyboard.TargetProperty="(StackPanel.Orientation)">
    				<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Orientation.Horizontal}"/>
    			</ObjectAnimationUsingKeyFrames>
    		</Storyboard>
    		<Storyboard x:Key="OnUnchecked1">
    			<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="stackPanel" Storyboard.TargetProperty="(StackPanel.Orientation)">
    				<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Orientation.Vertical}"/>
    			</ObjectAnimationUsingKeyFrames>
    		</Storyboard>
    	</Window.Resources>
    	<Window.Triggers>
    		<EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="toggleButton">
    			<BeginStoryboard Storyboard="{StaticResource OnChecked1}"/>
    		</EventTrigger>
    		<EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="toggleButton">
    			<BeginStoryboard x:Name="OnUnchecked1_BeginStoryboard" Storyboard="{StaticResource OnUnchecked1}"/>
    		</EventTrigger>
    	</Window.Triggers>
    
    	<Grid x:Name="LayoutRoot">
    		<StackPanel x:Name="stackPanel" >
    			<ToggleButton x:Name="toggleButton" Content="ToggleButton"/>
    			<Button Content="Button"/>
    			<Button Content="Button"/>
    			<Button Content="Button"/>
    		</StackPanel>
    	</Grid>
    </Window>
    



    Leon Terry
    Blog | TFK Labs | Twitter
    • Proposed as answer by VLTII Friday, July 1, 2011 11:28 AM
    • Marked as answer by view7 Friday, July 1, 2011 11:58 AM
    Friday, July 1, 2011 11:27 AM