결과 동영상 : https://www.youtube.com/watch?v=fLw5QcESnkk#action=share
The idea of doing this is to make the application comfortable for left handed and right handed people equally. We came across this problem when we started development of app for touchscreens. The RH people required the command grid on RHS and LH ppl on left handed respectively. There are two grids, One is a command grid (referred as commGrid) and the other is mainGrid. The mainGrid has a button Swap me! When the button is hit, the main grid and commGrid swap. the main window of the app (window1.xaml) has following grid ( superGrid).
- <grid x:name="superGrid">
-
- <grid.rowdefinitions>
-
- <rowdefinition></rowdefinition>
-
- </grid.rowdefinitions>
-
- <grid.columndefinitions>
-
- <columndefinition width="0.8*"></columndefinition>
-
- <columndefinition width="0.2*"></columndefinition>
-
- </grid.columndefinitions>
-
- <grid x:name="mainGrid" grid.column="0" margin="0,0,0,0" panel.zindex="1">
-
- <border borderthickness="5" cornerradius="20" borderbrush="Black">
-
- <button click="Button_Click" width="100" height="100">Swap me!</button>
-
- </border></grid>
-
- <grid x:name="commGrid" grid.column="1" margin="0,0,0,0" panel.zindex="99">
-
- <border borderthickness="5" cornerradius="20" borderbrush="Black">
-
- </border></grid>
-
- </grid>
The button_click event handler is as follows...
- private void Button_Click(object sender, RoutedEventArgs e)
-
- {
-
-
-
- double commGridWidth = commGrid.ActualWidth;
-
- double mainGridWidth = mainGrid.ActualWidth;
-
-
-
-
-
- if(animated)
-
- AnimateGrids(changeDexter, commGridWidth, mainGridWidth);
-
- else
-
- {
-
- GridChange(commGridWidth, mainGridWidth);
-
- changeDexter(this, EventArgs.Empty);
-
-
-
- }
-
-
-
-
-
- }
The most important function where the central logic lies is AnimateGrids. Since Margin does not have a animation by itself, we can use thickness animation. I have two animations, one for each grid.
- private void AnimateGrids(EventHandler postAnimation, double commGridWidth, double mainGridWidth)
-
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Storyboard sb = new Storyboard();
-
-
-
- ThicknessAnimation commGridAnimation
-
- = new ThicknessAnimation
-
- {
-
- From =
-
- new Thickness(0),
-
- To =
-
- new Thickness((isRightHanded ? -1 : 1) * mainGridWidth, 0,
-
- (isRightHanded ? 1 : -1) * mainGridWidth, 0),
-
- AccelerationRatio = 0.2,
-
- FillBehavior = FillBehavior.Stop,
-
- DecelerationRatio = 0.8,
-
- Duration = DURATION
-
- };
-
-
-
- ThicknessAnimation mainGridAnimation
-
- = new ThicknessAnimation
-
- {
-
- From =
-
- new Thickness(0),
-
- To =
-
- new Thickness((isRightHanded ? 1 : -1)*commGridWidth, 0,
-
- (isRightHanded ? -1 : 1)*commGridWidth, 0),
-
-
-
- FillBehavior = FillBehavior.Stop,
-
- AccelerationRatio = 0.2,
-
- DecelerationRatio = 0.8,
-
- Duration=DURATION
-
-
-
- };
-
-
-
-
-
- Storyboard.SetTarget(commGridAnimation, commGrid);
-
- Storyboard.SetTargetProperty(commGridAnimation,
-
- new PropertyPath(MarginProperty));
-
-
-
- Storyboard.SetTarget(mainGridAnimation, mainGrid);
-
- Storyboard.SetTargetProperty(mainGridAnimation,
-
- new PropertyPath(MarginProperty));
-
-
-
-
-
-
-
- sb.Children.Add(commGridAnimation);
-
- sb.Children.Add(mainGridAnimation);
-
-
-
- sb.Completed += new EventHandler(postAnimation);
-
-
-
- sb.Begin();
-
-
-
-
-
-
-
- }
ChangeDexter is an event handler which is defined as
- private void changeDexter(object sender, EventArgs e)
-
- {
-
- isRightHanded = !isRightHanded;
-
-
-
- Grid.SetColumn(mainGrid, isRightHanded ? 0 : 1);
-
- Grid.SetColumn(commGrid, isRightHanded ? 1 : 0);
-
-
-
-
-
- GridLength gl = superGrid.ColumnDefinitions.ElementAt(0).Width;
-
- superGrid.ColumnDefinitions.ElementAt(0).Width = superGrid.ColumnDefinitions.ElementAt(1).Width;
-
- superGrid.ColumnDefinitions.ElementAt(1).Width = gl;
-
-
-
-
-
-
-
- }
ChangeDexter is called at the end of animation.
|