09 June 2011

WPF - Store the position of a GridSplitter

Storing and restoring the position of a GridSplitter element in WPF is rather easy.
Hereby an example on a horizonal splitter:

The layout

<Grid.RowDefinitions>
  <Rowdefinition Height="32">
  <Rowdefinition Height="7*" x:Name="rowDefTables">
  <Rowdefinition Height="3*">
</Grid.RowDefinition>

Store the position

This line would run when the program closes.
Note that rowdefTables is the name of the RowDefinition.
The GridSplitter itself does not even need a name!
mSettings.SplitterTablesColumnsPosition =
          Convert.ToInt32(rowdefTables.Height.Value);
mSettings.SaveSettings(); //To a .ini file

Restore the position

This is done when the program starts:
mSettings.LoadSettings(); //From the .ini file
if (mSettings.SplitterTablesColumnsPosition > 0)
  rowdefTables.Height = new GridLength(
                        (double)mSettings.SplitterTablesColumnsPosition,
                        GridUnitType.Pixel);

No comments:

Post a Comment