I am creating a 3-column UI with grid splitters between the columns. I have the requirement to save the sate of the columns so that if the user closes and reopens the app it loo
So I believe I have figured this out. It is possible that some old values in my settings.settings were causing me issues, and it's possible that the default values I put in caused me issues. But here's what I did:
I'm not 100% convinced this is the best way, but it does seem to work, which makes me quite happy. In case anyone else has trouble and comes across this post, here is the working XAML:
That size changed event is still there only for debug tracing. I output the values of Width to see what is happening. Curiously, after expanding everything and going back to the window's minimum size, the right column width stays larger. But since they all have minwidths and the widths are all star-sized, it works itself out.
I did try to put this back into a binding, but since I'm now storing a string, and the GridLengthConverter is a TypeConverter, not an IValueConverter, it didn't work. I think it may be possible to store the values as GridLengths, though I've reached a point where I'm happy with what I've done. So my load and save are like this:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
//...
try
{
GridLengthConverter converter = new GridLengthConverter();
leftColumn.Width = (GridLength)converter.ConvertFromString(Settings.Default.MainWindowLeftColumnWidth);
centerColumn.Width = (GridLength)converter.ConvertFromString(Settings.Default.MainWindowCenterColumnWidth);
rightColumn.Width = (GridLength)converter.ConvertFromString(Settings.Default.MainWindowRightColumnWidth);
Trace.WriteLine(string.Format("LOADED Left: {0}, Center: {1}, Right {2}", leftColumn.Width, centerColumn.Width, rightColumn.Width));
}
catch (Exception)
{
// Fail silently, the worse case is we go with the defaults, it's going to be okay
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
//...
try
{
GridLengthConverter converter = new GridLengthConverter();
Settings.Default.MainWindowLeftColumnWidth = converter.ConvertToString(leftColumn.Width);
Settings.Default.MainWindowCenterColumnWidth = converter.ConvertToString(centerColumn.Width);
Settings.Default.MainWindowRightColumnWidth = converter.ConvertToString(rightColumn.Width);
Trace.WriteLine(string.Format("SAVED Left: {0}, Center: {1}, Right {2}", Settings.Default.MainWindowLeftColumnWidth, Settings.Default.MainWindowCenterColumnWidth, Settings.Default.MainWindowRightColumnWidth));
}
catch (Exception)
{
// Fail silently, the worst case is we don't save a little something, it's going to be okay
}
}
And that all worked for me. So I'm going to go with it!
EDIT: I later did some refinement to prevent the "curiosity" of the right column staying larger. I now have all panel size changes go to one event handler:
private void PanelSizeChanged(object sender, SizeChangedEventArgs e)
{
// In order to keep the resizing from losing proportionality, we'll force it to be proportional to the current size.
// Otherwise the right panel edges up and up while the other two remain at their starting 200*/300* values.
// And when that happens eventually resizing the window only resizes the right panel, not proportionately as it does at the start.
leftColumn.Width = new GridLength(leftColumn.ActualWidth, GridUnitType.Star);
centerColumn.Width = new GridLength(centerColumn.ActualWidth, GridUnitType.Star);
rightColumn.Width = new GridLength(rightColumn.ActualWidth, GridUnitType.Star);
}