473,385 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

wpf: scroll canvas to objects beyon top or left side.

I have a wpf project where I use a canvas to drag shapes around. when I
drag a shape beyond the right or bottom side I get scrollbars which is good.
I also get scrollbars when I zoom in and a shape goes beyond the right or
bottom side. However, I don't get scrollbars when shapes move beyond the
left or top side of the canvas. This is bad. I need the behavior similar
to Visio where you can drag an object past the left or top and you will get
scrollbars to scroll that area back into view.

I have a grid which has a scrollviewer and the canvas is the content of the
scrollviewer. In my custom canvas control at the end of the mouse move
event I call this line:

this.InvalidateMeasure();

Which causes the next method to run. All it does is re-measures the canvas
area which determines if the right or bottom are out of view. What can I do
here to determine if the top or left are out of view?

protected override Size MeasureOverride(Size constraint)
{
Size size = new Size();
foreach (UIElement element in base.Children)
{
double left = Canvas.GetLeft(element);
double top = Canvas.GetTop(element);
left = double.IsNaN(left) ? 0 : left;
top = double.IsNaN(top) ? 0 : top;

//measure desired size for each child
element.Measure(constraint);

Size desiredSize = element.DesiredSize;
if (!double.IsNaN(desiredSize.Width) &&
!double.IsNaN(desiredSize.Height))
{
size.Width = Math.Max(size.Width, left + desiredSize.Width);
size.Height = Math.Max(size.Height, top + desiredSize.Height);
}
}
// add margin
size.Width += 10;
size.Height += 10;
return size;
}
Note, most of what I do is in c# rather than xaml.

Thanks.
--
mo*******@newsgroup.nospam
Jun 27 '08 #1
4 10063
Hi George,

Could you please reproduce the problem in a simple application and send it
to me?I think it would be a quick way that helps to address the problem.

To get my actual email address, remove 'online' from my displayed email
address.

Thank you for your understanding and cooperation. I look forward to your
reply!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #2
Hi George,

Thank you for your sample project! I understand what you're trying to
achieve now.

The reason why the scrollbars of the ScrollViewer don't appear when the
Rectangle is moved beyond the left or the top side of the Canvas is that
the Canvas's size isn't changed bigger than the viewport size of the
ScrollViewer at all.

The following steps can lead you to what you want:
1. Among all child elements in the Canvas, find the minimum left and top
values of the child elements.
2. If the minimun left or top value is less than zero, adjust positions of
all child elements according to the minimum left or top values.
3. Calculate the desired size of the Canvas based on all elements in it.
4. Scroll the parent ScrollViewer the absolute value of the minimum left
and top value horizontally and vertically, if any.

I have made some modifications in the override MeasueOverride method of the
DiagramCanvas class and it works well on my side. I will send the modified
sample project to your email box.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #3
Hi George,

Thank you for your feedback!

Although you place a Thumb in the Canvas in the project, the main idea to
get the scrollbars to appear in the ScrollViewer remains the same. I modify
my code for the override MeasureOverride method as follow and it works fine
on my side:

protected override Size MeasureOverride(Size constraint)
{
Size size = new Size();
// restore the left and top values of all Buttons to their
original values
foreach (UIElement element in base.Children)
{
if (!(element is Thumb))
{
if (horizontaloffset < 0.0)
{
Canvas.SetLeft(element, Canvas.GetLeft(element) +
horizontaloffset);
}
if (verticaloffset < 0.0)
{
Canvas.SetTop(element, Canvas.GetTop(element) +
verticaloffset);
}
}
}
// get the parent ScrollViewer, if any
ScrollViewer sv = null;
if (this.Parent != null && (this.Parent is ScrollViewer))
{
sv = this.Parent as ScrollViewer;
}

double minleft = 0.0, mintop = 0.0, minwidth = 0.0, minheight =
0.0;
if (sv != null)
{
// find the minimun left and top values of all child
elements
foreach (UIElement element in base.Children)
{
minleft = Math.Min(minleft, Canvas.GetLeft(element));
mintop = Math.Min(mintop, Canvas.GetTop(element));
}
// store minimum left and top values to the global variables
horizontaloffset = minleft;
verticaloffset = mintop;
// if the minimum left value is less than zero, we can
determine the minimum width of the Canvas
if (minleft < 0.0)
{
minwidth = sv.ViewportWidth + Math.Abs(minleft);
}
// if the minimum top value is less than zero, we can
dertermine the minimum height of the Canvas
if (mintop < 0.0)
{
minheight = sv.ViewportHeight + Math.Abs(mintop);
}
}
// set initial values to Width and Height of the size variable
size.Width = minwidth;
size.Height = minheight;
foreach (UIElement element in base.Children)
{
// if minimum left value is less than zero, adjust each
element's left value
if (minleft < 0.0)
{
Canvas.SetLeft(element, Canvas.GetLeft(element) +
Math.Abs(minleft) );
}
// if minimum top value is less than zero, adjust each
element's top value
if (mintop < 0.0)
{
Canvas.SetTop(element, Canvas.GetTop(element) +
Math.Abs(mintop) );
}

double left = Canvas.GetLeft(element);
double top = Canvas.GetTop(element);
left = double.IsNaN(left) ? 0 : left;
top = double.IsNaN(top) ? 0 : top;

//measure desired size for each child
element.Measure(constraint);

Size desiredSize = element.DesiredSize;
// calculate the desired size of the Canvas
if (!double.IsNaN(desiredSize.Width) &&
!double.IsNaN(desiredSize.Height))
{
size.Width = Math.Max(size.Width, left +
desiredSize.Width);
size.Height = Math.Max(size.Height, top +
desiredSize.Height);
}
}

// add margin
size.Width += 300;
size.Height += 300;

// scroll parent if any
if (sv != null)
{
sv.ScrollToHorizontalOffset(Math.Abs(minleft) );
sv.ScrollToVerticalOffset(Math.Abs(mintop));
}
return size;
}

I will send the modified sample project to your email box. If you have any
question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #4
Hi George,

Thank you for your feedback! I'm glad to hear that the problem is solved
now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Raymond H. | last post by:
Hello, in my vb projet, I put a WebBrowser and when I view the contents of my hard disk with 'WebBrowser1.Navigate "C:\"' I view my directory in the right side only and in the left side it is'nt...
0
by: gallery | last post by:
I'm trying to get this page ready to become a DW Template .... am almost there .... It validates OK and seems fine in IE 6 on Windows XP at all sizes of windows. In an 800px wide window, all...
0
by: John T | last post by:
I have the code I need to show a sort icon in the column header of a ListView control. The issue I have is that the image is always on the right side of the column header text. This isn't...
1
by: UJ | last post by:
I've got a div with scrolling enabled. It's wider than the screen so the scroll bars are off the screen and somebody have to scroll the page over. Is there any way to put scroll bars on the left...
5
by: Amit_Basnak | last post by:
Dear Friends I have been getting the following error Error 185: "WorkFlow_dce.cpp", line 58 # Left side of '->' requires a pointer to class; type found was 'struct WF_SEARCH_WU'....
1
by: mzkpk | last post by:
Hi.. i m generating PDF file by using FPDF class of php it is successfully generated but i want to set image on left side and text on right side but when i display the image the text go to the bottom...
0
by: wildman | last post by:
RE: menu width and background color ? Jusitify menu items on left side?? I probably need to do this a table or span.. I have a horizontal menu i want to be 800 wide with the same background...
3
by: Zhang Weiwu | last post by:
Dear all I got the following HTML: <div id="div1" style="width: 100%; overflow: hidden;"> <table>...</table> </div> The table element is controlled by javascript and grows by width during...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.