Create custom rectangle UI Element

M

moondaddy

I want to create a custom UI element which will be a custom rectangle object
in a c# XAML application. I'm new to XAML and c# as most of my experience
has been using vb with sql. I'm building a simple Visio type app where I
can drag rectangles on a canvas to create diagrams. for now I don't need
fancy shapes so I'll just start with a rectangle class where I can add
custom properties to it such as where lines are connected to it, etc. Can
someone please recommend some documentation on how to get started with
creating this custom rectangle class. I'm not sure how to get started such
as creating a class that inherits from rectangle or what...

Thanks.
 
W

Walter Wang [MSFT]

Hi,

I'm not sure if you're referring to System.Windows.Shaps.Rectangle:

#Rectangle Class (System.Windows.Shapes)
http://msdn2.microsoft.com/en-us/library/system.windows.shapes.rectangle.asp
x

This class cannot be inherited. It's actually only a special Shape:

#Shape Class (System.Windows.Shapes)
http://msdn2.microsoft.com/en-us/library/system.windows.shapes.shape.aspx
Provides a base class for shape elements, such as Ellipse, Polygon, and
Rectangle. This class is abstract.

The ultimate base class of them is the UIElement class:

#UIElement Class (System.Windows)
http://msdn2.microsoft.com/en-us/library/system.windows.uielement.aspx
UIElement is a base class for WPF core level implementations building on
Windows Presentation Foundation (WPF) elements and basic presentation
characteristics.


To create a visio like application, you need to manage many objects of type
UIElement, not a specific type Rectangle.

I suggest you start with following example:

#Dragging Elements in a Canvas - The Code Project - Windows Presentation
Foundation
http://www.codeproject.com/WPF/DraggingElementsInCanvas.asp

Then choose relevant samples from:

#Download details: Windows SDK .NET Framework 3.0 Samples
http://www.microsoft.com/downloads/details.aspx?FamilyID=22b58b6c-8f98-40d0-
880d-c3339c5da01e&displaylang=en


Hope this helps.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

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/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

Thanks Walter,

I've seen these article and will continue to study them. Can you recommend
what the best type of class to use for my custom shapes may be? I've
started off by building a class which inherits from Canvas and I'm using
this for my rectangles. I chose to inherit the Canvas class as I thought it
would be flexible for future needs such as hosting various child objects
like lists or what ever. However, it may not be the best if I want to give
it a real gui look and feel. Maybe the shape class would be better for
achieving a nice look and feel, but might be limited for hosting child
objects. What's your opinion?

Thanks.
 
W

Walter Wang [MSFT]

Hi,

I think Josh Smith's DragCanvasDemo should be really be useful for a visio
like application.

Based on my understanding, as you described in your first message, you want
to create a custom rectangle to store the connection points used to connect
different rectangles. I don't think create a custom shape or rectangle is
the best way to do.

In WPF, such properties can be implemented using Attached Properties.
Taking the DragCanvasDemo for example, the CanBeDragged property you're
seeing attached to each UIElement is actually defined the the
container--the customized Canvas here. I think you can also define other
Attached Properties in the Canvas object and all the contained UIElement
objects can have these attached properties to store various settings. You
don't have to inherit from Shape to create a customized element.

I think the DragCanvasDemo example and Josh's another example on drawing a
line between movable elements will be useful for you:

#Drawing a Line Between Movable Elements
http://www.infusionblogs.com/blogs/jsmith/archive/2006/11/28/7069.aspx

For more information on Attached Properties and Dependency Properties,
please refer to MSDN documentation:

#Properties
http://msdn2.microsoft.com/en-us/library/ms753192.aspx


Also, these blogs might be helpful for you to understand Attached
Properties:

#Josh Smith on WPF : Attached Properties
http://www.infusionblogs.com/blogs/jsmith/archive/2006/07/07/638.aspx

#Nick on WPF/E : Attached properties
http://blogs.msdn.com/nickkramer/archive/2005/08/25/456024.aspx


Hope this helps.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

Thanks Walter. I'll study these resources and if I have further questions
I'll get back to you.

Merry Christmas and Happy Holidays!
 
W

Walter Wang [MSFT]

Thanks for the update and Merry Christmas!

In case of questions, please feel free to reply here or create a new post.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top