locked
How to persist splitter location for each user? RRS feed

  • Question

  • I know how to use isolated storage for user preferences, but I can't seem to find info on whether it is possible to retrieve and set the current location of the horizontal and vertical splitter controls? Is this possible?

    Thanks,
    Roger

    Monday, July 13, 2015 4:17 PM

Answers

  • Hi Rdogmartin,

    In LightSwitch Desktop application, we can use FindControl method to get all controls on screen with controls' name. The Splitter is part of Grid which is used to layout screen. Following code shows how I get columns' ActualWidth in the screen layout Grid. We can record this value and set the Width when customer open this screen next time.

    partial void CustomerInfoesListDetail_Created() 
    {
         this.FindControl("ScreenLayout").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(gdMyGrid_ControlAvailable);
    }
     
    private void gdMyGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
        Grid grid = (Grid)e.Control;
        double a= grid.ColumnDefinitions[0].ActualWidth;
        double b = grid.ColumnDefinitions[1].ActualWidth;
    }

      




    Best Regards,
    Weiwei

    • Proposed as answer by Roger Martin (MSDN) Tuesday, July 28, 2015 10:18 PM
    • Marked as answer by Angie Xu Monday, August 3, 2015 5:05 AM
    Monday, July 27, 2015 8:10 AM
    Moderator
  • Thank you! With your code as a starting point, I was able to implement it. Here is the code I used for retrieving and storing the position of a vertical splitter and a horizontal splitter:

    private Grid _grdScreenLayout;
    private Grid _grdDemandItemDetails;
    private bool _grdScreenLayout_ControlAvailableHandled;
    private bool _grdDemandItemDetails_ControlAvailableHandled;
    
    partial void DemandItems_Created()
    {
      this.FindControl("grdScreenLayout").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(grdScreenLayout_ControlAvailable);
      this.FindControl("grdDemandItemDetails").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(grdDemandItemDetails_ControlAvailable);
    }
    
    partial void DemandItems_Closing(ref bool cancel)
    {
     Dispatchers.Main.BeginInvoke(PersistUserPreferences);
    }
    		
    private void grdScreenLayout_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
     if (!_grdScreenLayout_ControlAvailableHandled)
     {
      _grdScreenLayout = (Grid)e.Control;
    
      double taskRowHeight;
      if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("TaskRowHeight", out taskRowHeight))
      {
       _grdScreenLayout.RowDefinitions[3].Height = new GridLength(taskRowHeight);
      }
    
      _grdScreenLayout_ControlAvailableHandled = true;
     }
    }
    
    private void grdDemandItemDetails_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
     if (!_grdDemandItemDetails_ControlAvailableHandled)
     {
      _grdDemandItemDetails = (Grid)e.Control;
    
      double taskColWidth;
      if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("TaskColumnWidth", out taskColWidth))
      {
       // Set the width of the demand task column (the one that has the packout tab). We need to reduce the width
       // to ensure the splitter bar is always visible. This is necessary if the user drags the splitter all the
       // way to the right (which makes it disappear) or opens the screen in a smaller window or monitor than 
       // when the setting was persisted. For some reason this monkey business isn't necessary when setting the
       // row height in grdScreenLayout_ControlAvailable - in that case the splitter is always visible no matter what.
       var gridWidth = _grdDemandItemDetails.ColumnDefinitions.Sum(c => c.ActualWidth);
    
       _grdDemandItemDetails.ColumnDefinitions[0].Width = new GridLength(Math.Min(taskColWidth, gridWidth - 10));
      }
    
      _grdDemandItemDetails_ControlAvailableHandled = true;
     }
    }
    
    public void PersistUserPreferences()
    {
     IsolatedStorageSettings.ApplicationSettings["TaskRowHeight"] = _grdScreenLayout.RowDefinitions[3].ActualHeight;
     IsolatedStorageSettings.ApplicationSettings["TaskColumnWidth"] = _grdDemandItemDetails.ColumnDefinitions[0].ActualWidth;
    }

    • Marked as answer by Angie Xu Monday, August 3, 2015 5:05 AM
    Tuesday, July 28, 2015 10:17 PM

All replies

  • Hi Rdogmartin,

     

    In my opinion, we can create Splitter in the render event as below. Please try to set the Margin or Padding in the Style when you create the splitter. We also could get all controls on the screen, of course included Splitter. About how to get all controls on screen, please refer to following thread. Hope that can help you.
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/65b146d3-2bee-44ed-a4e6-e54383627756/is-there-a-way-to-programatically-browse-all-controls-on-a-screen?forum=lightswitch

    var splitter = $("<div id='vsplitter1' style='width:200px;height:300px'><div>panel1</div><div>panel2</div></div>");
    splitter.appendTo($(element));
    splitter.wijsplitter({ orientation: "vertical" });

      

    Best Regards,
    Weiwei
    Tuesday, July 14, 2015 7:47 AM
    Moderator
  • I'm sorry - I should have mentioned this is in the Silverlight desktop client, not HTML.
    Tuesday, July 14, 2015 2:05 PM
  • Hi Rdogmartin,

    Sorry for misunderstanding.
    Are you using GridSplitter? If not, please tell us which Splitter are you using. If you are using GridSplitter, I think below thread will help you to retain the position of Splitter that a user has set.
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/08579b70-8b53-4855-9eea-333ce05491f8/how-to-saverestore-gridsplitter-position
     
    Best Regards,
    Weiwei

    Wednesday, July 15, 2015 1:36 AM
    Moderator
  • I don't know what it is under the hood. It's the splitter LightSwitch automatically uses when you check the "Is Row Resizable" and "Is Column Resizable" properties in the designer. See Beth Massi's blog post LightSwitch Screen Tips: Scrollbars and Splitters to see what I mean.
    Wednesday, July 15, 2015 1:56 AM
  • Hi Rdogmartin,

    In LightSwitch Desktop application, we can use FindControl method to get all controls on screen with controls' name. The Splitter is part of Grid which is used to layout screen. Following code shows how I get columns' ActualWidth in the screen layout Grid. We can record this value and set the Width when customer open this screen next time.

    partial void CustomerInfoesListDetail_Created() 
    {
         this.FindControl("ScreenLayout").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(gdMyGrid_ControlAvailable);
    }
     
    private void gdMyGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
        Grid grid = (Grid)e.Control;
        double a= grid.ColumnDefinitions[0].ActualWidth;
        double b = grid.ColumnDefinitions[1].ActualWidth;
    }

      




    Best Regards,
    Weiwei

    • Proposed as answer by Roger Martin (MSDN) Tuesday, July 28, 2015 10:18 PM
    • Marked as answer by Angie Xu Monday, August 3, 2015 5:05 AM
    Monday, July 27, 2015 8:10 AM
    Moderator
  • Thank you! With your code as a starting point, I was able to implement it. Here is the code I used for retrieving and storing the position of a vertical splitter and a horizontal splitter:

    private Grid _grdScreenLayout;
    private Grid _grdDemandItemDetails;
    private bool _grdScreenLayout_ControlAvailableHandled;
    private bool _grdDemandItemDetails_ControlAvailableHandled;
    
    partial void DemandItems_Created()
    {
      this.FindControl("grdScreenLayout").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(grdScreenLayout_ControlAvailable);
      this.FindControl("grdDemandItemDetails").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(grdDemandItemDetails_ControlAvailable);
    }
    
    partial void DemandItems_Closing(ref bool cancel)
    {
     Dispatchers.Main.BeginInvoke(PersistUserPreferences);
    }
    		
    private void grdScreenLayout_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
     if (!_grdScreenLayout_ControlAvailableHandled)
     {
      _grdScreenLayout = (Grid)e.Control;
    
      double taskRowHeight;
      if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("TaskRowHeight", out taskRowHeight))
      {
       _grdScreenLayout.RowDefinitions[3].Height = new GridLength(taskRowHeight);
      }
    
      _grdScreenLayout_ControlAvailableHandled = true;
     }
    }
    
    private void grdDemandItemDetails_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
     if (!_grdDemandItemDetails_ControlAvailableHandled)
     {
      _grdDemandItemDetails = (Grid)e.Control;
    
      double taskColWidth;
      if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("TaskColumnWidth", out taskColWidth))
      {
       // Set the width of the demand task column (the one that has the packout tab). We need to reduce the width
       // to ensure the splitter bar is always visible. This is necessary if the user drags the splitter all the
       // way to the right (which makes it disappear) or opens the screen in a smaller window or monitor than 
       // when the setting was persisted. For some reason this monkey business isn't necessary when setting the
       // row height in grdScreenLayout_ControlAvailable - in that case the splitter is always visible no matter what.
       var gridWidth = _grdDemandItemDetails.ColumnDefinitions.Sum(c => c.ActualWidth);
    
       _grdDemandItemDetails.ColumnDefinitions[0].Width = new GridLength(Math.Min(taskColWidth, gridWidth - 10));
      }
    
      _grdDemandItemDetails_ControlAvailableHandled = true;
     }
    }
    
    public void PersistUserPreferences()
    {
     IsolatedStorageSettings.ApplicationSettings["TaskRowHeight"] = _grdScreenLayout.RowDefinitions[3].ActualHeight;
     IsolatedStorageSettings.ApplicationSettings["TaskColumnWidth"] = _grdDemandItemDetails.ColumnDefinitions[0].ActualWidth;
    }

    • Marked as answer by Angie Xu Monday, August 3, 2015 5:05 AM
    Tuesday, July 28, 2015 10:17 PM
  • Hi Rdog,

    Thanks for sharing your solution. It is beneficial to others who encounter the similar issue, if you have any difficulty in future, welcome to our forum.

     

    Best Regards,

    Weiwei



    Thursday, July 30, 2015 2:33 AM
    Moderator