Window window = VisualTreeHelperExtended.GetRoot(toolWindow) as Window ?? LogicalTreeHelperExtended.GetRoot(toolWindow) as Window;
if (null != window) {
window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size desiredSize = window.DesiredSize;
window.Width = Math.Ceiling(desiredSize.Width);
window.Height = Math.Ceiling(desiredSize.Height);
window.Measure(new Size(window.Width, window.Height));
}
Hi,
I am trying to achieve the same behavior. My floating tool window shows or hides some controls depending on the mode and the size of the container should be updated automatically.
I assumed that setting SizeToContentModes="Floating" would do the trick, but it doesn't work for me. Am I missing something?
Your code snippet to change the window size works, but has the described side effect.
Hi Tobias,
The SizeToContentModes="Floating" option should size the root Window to fit the desired size of the tool window, but it only occurs when the tool window is first opened. After that, it won't automatically update.
Which side effect were you referring to?
Okay, then SizeToContentModes won't work for me.
I am refering to the case where the tool window is docked with another window:
But it may not work perfectly in all cases (such as when you have two ToolWindows docked side-by-side).
In that case I don't need to resize the window. But how do I find out if its the only tool window in the container?
You might be able to walk up the visual tree and see the ToolWindow.ToolWindowContainer (if not null) is in an ancestor SplitContainer. If so, check for any other containers in SplitContainer.Children to see if there are any ToolWindowContainer instances that have their Windows collection populated. The ToolWindowContainer.Windows collection may be empty if you had a tool window in a container but then closed the tool window. In that case, a breadcrumb is left behind.
Okay, I think this will work:
private static bool IsToolWindowAloneInContainer(DockingWindow dockingWindow)
{
var splitContainer = VisualTreeHelperExtended.GetAncestor<SplitContainer>(dockingWindow);
if (splitContainer == null)
{
if (dockingWindow.ParentContainer is ToolWindowContainer container)
{
return container.Windows.Count == 1;
}
return true;
}
return VisualTreeHelperExtended.GetAllDescendants<ToolWindowContainer>(splitContainer).Count <= 1;
}
Hi Tobias,
We are adding a DockHost.GetVisibleToolWindowContainerCount() method in the upcoming build that will help you and have some improved logic. I'd recommend switching to that once it's available. You can get the current DockHost from the DockingWindow.DockHost property.
Please log in to a validated account to post comments.