Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Diagram autoresize to fit ScrollViewer (Read 9828 times)
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Diagram autoresize to fit ScrollViewer
Oct 31st, 2013 at 3:20pm
Print Post  
Hi Stoyan,

I have a user control with a ScrollViewer and within it a Diagram object. I've set the Diagram.Width and Diagram.Height to Auto in the xaml file. But when I increase the window size to full screen, the ScrollViewer resizes to fit the screen but the Diagram does not. Is there a way to make it autoresize?

Thanks in advance!

Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #1 - Nov 1st, 2013 at 10:47am
Print Post  
Hi Vincent,

The Diagram's MeasureOverride method returns the size of Diagram.Bounds, and disregards the Width and Height properties of FrameworkElement. You will have to set Bounds to a value that matches the ScrollViewer's dimensions, or if you use a custom Diagram class, you could override the MeasureOverride method and return the larger of Diagram.Bounds.Size and ScrollViewer's size.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #2 - Nov 1st, 2013 at 12:21pm
Print Post  
Hi Stoyan,

Thanks for the suggestion. I'm using the following code to check for the larger Size.

Code
Select All
protected override Size MeasureOverride(Size availableSize)
        {
            Size desiredSize = new Size();
            UIElement parent = VisualTreeHelper.GetParent(this) as UIElement;
            ScrollViewer sv = null;
            if (parent is ScrollViewer)
                sv = parent as ScrollViewer;
            if (sv != null)
            {
                Size svSize = sv.DesiredSize;
                double x1 = this.Bounds.Size.Width;
                double x2 = svSize.Width;
                if (x1 > x2)
                    desiredSize = this.Bounds.Size;
                else
                    desiredSize = svSize;
            }
            else
            {
                desiredSize = this.Bounds.Size;
            }
            return desiredSize;
        }
 



Unfortunately, this isn't working. I've added a background color (blue) to the scrollviewer to see where the diagram is placed. (Can be seen in the image below)



If I click on the <pr:BRDiagram /> element in the xaml modeler in VS, it shows that the width of the element is the width of the ScrollViewer. But it seems the Diagram field itself isn't expanding. How can I fix this?

Thanks in advance!

Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #3 - Nov 1st, 2013 at 1:22pm
Print Post  
What does the debugger show for ScrollViewer.DesiredSize if you set a breakpoint on that line? I think DesiredSize will not have a valid value until the ScrollViewer's own measure method returns (and it's currently calling the diagram's method most likely).
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #4 - Nov 1st, 2013 at 2:09pm
Print Post  
svSize i.e. ScrollViewer.DesiredSize = {0;0} in debugger
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #5 - Nov 1st, 2013 at 2:50pm
Print Post  
So you are always returning Bounds.Size if I'm reading that code correctly. Check if the availableSize argument passed to MeasureOverride by ScrollViewer has the expected size and use it instead of DesiredSize.
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #6 - Nov 1st, 2013 at 3:03pm
Print Post  
availableSize = {infinity, infinity}

If I try this, I get an error: "InvalidOperationException: Layout measurement override of element 'TSFBRModeler.TSFBRPresentation.BRDiagram' should not return PositiveInfinity as its DesiredSize, even if Infinity is passed in as available size."
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #7 - Nov 1st, 2013 at 3:16pm
Print Post  
Don't you have some fixed size higher up in the visual tree that you could use instead of these values?
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #8 - Nov 4th, 2013 at 7:47am
Print Post  
I have a column definition with a set percentage of the total width.

My code for the xaml file is:

Code
Select All
<UserControl x:Class="TSFBRModeler.TSFBRScreens.BRModeler"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:diag="http://mindfusion.eu/diagramming/wpf"
             xmlns:pr="clr-namespace:TSFBRModeler.TSFBRPresentation"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="1500"
             Height="Auto" Width="Auto">
    <Grid>
        <!--<Grid.RowDefinitions>
            <RowDefinition Height="464"/>
            <RowDefinition Height="0"/>
        </Grid.RowDefinitions>-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="13*"/>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition Width="12*"/>
        </Grid.ColumnDefinitions>

        <pr:BRAccordionLeft Grid.Column="0"/>
        <ScrollViewer x:Name="diagramScrollViewer"
                      Grid.Column="1"
                      HorizontalScrollBarVisibility="Auto"
                      Focusable="False"
                      Background="#FF2727E4">
            <pr:BRDiagram x:Name="modelerDiagram" Width="Auto" Height="Auto"/>
        </ScrollViewer>
        <!--.HorizontalScrollBarVisibility="Visible"
                      Height="700" Grid.RowSpan="2"-->
        <pr:BRAccordionRight Grid.Column="2"/>
    </Grid>
</UserControl>
 



I expected the diagram field to fill the available space but i do see the background color of the scrollviewer.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #9 - Nov 4th, 2013 at 12:30pm
Print Post  
Unless you return at least that column's actual width from MeasureOverride, you will see the ScrollViewer's background. It seems WPF's layout system does not support very well what you want to achieve, so try setting ScrollViewer.Background to diagram's BackBrush instead.

Or you might try implementing that in two passes. First time return a large size from MeasureOverride, then record ScrollViewer's ActualWidth and ActualHeight values, then invalidate the layout and start returning those recorded values from BRDiagram.MeasureOverride.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #10 - Nov 4th, 2013 at 3:42pm
Print Post  
Stoyo wrote on Nov 4th, 2013 at 12:30pm:
Or you might try implementing that in two passes. First time return a large size from MeasureOverride, then record ScrollViewer's ActualWidth and ActualHeight values, then invalidate the layout and start returning those recorded values from BRDiagram.MeasureOverride.


I'm not sure what you mean by two passes. From where would I call MeasureOverride the first time? Also BRDiagram?

Thanks in advance!

Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Diagram autoresize to fit ScrollViewer
Reply #11 - Nov 5th, 2013 at 8:41am
Print Post  
I meant you should force a second WPF layout pass by calling InvalidateMeasure. However it's of no use, I tried it and the result is that the diagram still draws its background and grid points only within diagram.Bounds, and not in the last measured size returned by MeasureOverride. So you will have to modify Bounds whenever you want the diagram to fill parent ScrollViewer entirely, e.g. try this:

Code
Select All
private void Window_Loaded(object s, RoutedEventArgs e)
{
	originalBounds = diagram.Bounds;
	scrollViewer.SizeChanged += OnScrollViewerSizeChanged;
}

Rect originalBounds;

private void OnScrollViewerSizeChanged(object sender, SizeChangedEventArgs e)
{
	var scrollViewer = (ScrollViewer)sender;

	var diagBounds = diagram.Bounds;
	diagBounds.Width = scrollViewer.ActualWidth > diagram.Bounds.Width ?
		scrollViewer.ActualWidth : originalBounds.Width;
	diagBounds.Height = scrollViewer.ActualHeight > diagram.Bounds.Height ?
		scrollViewer.ActualHeight : originalBounds.Height;
	diagram.Bounds = diagBounds;
} 



You will have to update the originalBounds every time you assign to diagram.Bounds or call ResizeToFitItems().

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Diagram autoresize to fit ScrollViewer
Reply #12 - Nov 5th, 2013 at 12:50pm
Print Post  
Finally it works, thanks! Cheesy
All hail Stoyan!!

Greets,

Vincent
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint