I have 2 columns in a Grid. When I click a button, I want the first column to animate to the left from it's current position to 0. So, in effect, it collapses and I'm left with just viewing a single column.
相关问题
- VNC control for WPF application
- Tkinter Grid Columnspan ignored
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
I have taken Todd Miranda's C# source code and modified it, to demonstrate how to animate DataGrid Column widths shrinking & expanding.
Here's the source code...
http://www.pocketpctoolkit.com/WPF/DataGridColumnWidthAnimation.zip
Basically, you click on a CheckBox, and whichever DataGrid columns have their "MinWidth" value set to 0 will be shrunk to zero-width. Click the CheckBox again, the columns will animate back to their original widths.
This WPF code also demonstrates how to create animations / storyboards in code behind.
You can also use the Reveal control from Kevin's bag-o-tricks, http://j832.com/bagotricks/
Check out this video training link, from Todd Miranda that shows you how to animate the height of a grid control. I think you could easily make it work for your case.
Another thing you can do is animate the contents and set the Grid to autosize to content which it will do smoothly as the contents changes size.
You can also achieve this with GridLength animation , see an example here http://marlongrech.wordpress.com/2007/08/20/gridlength-animation/ Using this approach you can manipulate any given Grid.Column or Grid.Row size.
For your special need just put first column with Width="Auto" and second with *, animate the with of the content inside the first column- that will do the trick.
Shouldn't be too hard. You'd need to create an EventTrigger that has a BeginStoryboard that targets the grid and uses a DoubleAnimation to shrink the column width. The example here has a similar setup. The EventTrigger would go on the button and the DoubleAnimation's StoryBoard.Target would point to the ColumnDefinition you wish to shrink.
Okay, so that doesn't work so well. You can't shrink the column directly, but you CAN set the shrinking column to fill (width="*"), set the width of the Grid and the non-shrinking column, and then shrink the entire grid. This does work. The below example works: