Custom Shape Binding

7 posts, 0 answers
  1. Egemen
    Egemen avatar
    34 posts
    Member since:
    Apr 2012

    Posted 01 Apr 2013 Link to this post

    Hi,

    I'd like to bind an ObservableCollection<CustomShape> to RadDiagram in Xaml like {Binding MyShapes}.Is it possible to do this?

  2. Tom
    Tom avatar
    16 posts
    Member since:
    Sep 2012

    Posted 05 Apr 2013 Link to this post

    RadDiagram definitely supports MVVM, but  my understanding is that you have to bind to something derived from the GraphSourceBase<TNode, TLink> base class (or IGraphSource) .     You'll just need to transform your collection into the list of Nodes.

    See this...
    http://www.telerik.com/help/silverlight/raddiagrams-howto-mvvm.html
  3. DevCraft banner
  4. Egemen
    Egemen avatar
    34 posts
    Member since:
    Apr 2012

    Posted 15 Apr 2013 Link to this post

    Hi,

    Yes, i know this article but my custom shapes are already derived from RadDiagramShapeBase... When i tried to use IGraphSource,
    i have an exception because i don't need to implement the methods about Links. I tried to implement a fake implementation but i had got some internal exceptions...

    Thank you for your help.
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 18 Apr 2013 Link to this post

    Hello Egemen,

    When working with MVVM it is best to follow its main concept - separate the UI from the business logic. And if you create a business collection of custom shapes that derive from RadDiagramShapeBase, then you're breaking this concept as the RadDiagramShapeBase is essentially an UIElement. And I believe this is why you've experienced issues with this approach.

    And if your goal is to bind the RadDiagram GraphSource to a collection of business data, then the article Tom mentioned is the way to go. The GraphSource described in it uses NodeViewModelBase and LinkViewModelBase classes which are data classes that are designed to describe the nodes and links in a diagram. Then once you bind the RadDiagram.GraphSource to the business collection, it will automatically create RadDiagramShape/RadDiagramConnection objects to wrap the
    NodeViewModelBase/LinkViewModelBase items. But if you need to wrap the NodeViewModelBase items in CustomShapes, then you have to create a custom RadDiagram. In the custom diagram class you need to override the IsItemItsOwnShapeContainerOverride and GetShapeContainerForItemOverride methods to return the custom shape type:
    public class CustomDiagram : RadDiagram
    {
        protected override bool IsItemItsOwnShapeContainerOverride(object item)
        {
            return item is CustomShape;
        }
     
        protected override Telerik.Windows.Diagrams.Core.IShape GetShapeContainerForItemOverride(object item)
        {
            return new CustomShape();
        }
    }

    This approach is described at the bottom of the How to Create Custom Shape article.

    Kind regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Egemen
    Egemen avatar
    34 posts
    Member since:
    Apr 2012

    Posted 18 Apr 2013 Link to this post

    Hi,

    I haven't got any business data. My goal is just  to bind CustomShape collection in the XMAL like this {Binding MyShapes}. Tom mentioned that l just need to transform your collection into the list of Nodes.
    Can you please tell me how i can do this ? Could you provide me an example please...

    Thank you
  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 19 Apr 2013 Link to this post

    Hello Egemen,

    Please take a look at this forum post as it describes such a scenario.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Egemen
    Egemen avatar
    34 posts
    Member since:
    Apr 2012

    Posted 24 Apr 2013 Link to this post

    Thank you very much for your help.
Back to Top
DevCraft banner