This is a migrated thread and some comments may be shown as answers.

Two different custom shapes in RadDiagram

1 Answer 234 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 08 Aug 2014, 08:49 AM
In my RadDiagram, I currently have the following XAML:

<telerik:RadDiagram.ShapeStyle>
  <Style TargetType="telerik:RadDiagramShape">
    <Setter Property="Geometry" Value="{Binding Geometry, Mode=TwoWay}"></Setter>
  </Style>
</telerik:RadDiagram.ShapeStyle>

<telerik:RadDiagram.ShapeTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding Header}" ></TextBlock>
  </DataTemplate>
</telerik:RadDiagram.ShapeTemplate>

Now I want something another shape, with the same style, but with a ShapeTemplate like:

<telerik:RadDiagram.ShapeTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding Header}" ></TextBlock>
    <TextBlock Text="{Binding SomeValue}" ></TextBlock>
    <TextBlock Text="{Binding AnotherHeader}" ></TextBlock>
  </DataTemplate>
</telerik:RadDiagram.ShapeTemplate>

Which can also be edited (only SomeValue):
<telerik:RadDiagram.ShapeEditTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding Header}" ></TextBlock>
    <TextBox Text="{Binding SomeValue, Mode=TwoWay}" />
    <TextBlock Text="{Binding AnotherHeader}" ></TextBlock>
  </DataTemplate>
</telerik:RadDiagram.ShapeEditTemplate>
 
How can I accomplish having both types of shapes in the same Diagram?
Can this be done in XAML, or do I have to do this in code-behind?
For both types of solutions: How can I accomplish this?

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 12 Aug 2014, 07:13 AM
Hi Jason,

The WPF framework provides a solution for the scenarios when you have a control that displays a collection of items and you want to apply different template for the different types of items. Most of the items controls exposes a property ItemTemplate that defines a template for the items. Some of them also exposes ItemTemplateSelector property that accepts a class that derives from the DataTemplateSelector class. You can see how to use DataTemplateSelector in the corresponding MSDN article.

The ShapeTemplate property of the RadDiagram has corresponding ShapeTemplateSelector, the same applies for the ShapeEditTemplate, there is ShapeEditTemplateSelector. Basically you can create a class that derives from the DataTemplate selector and override its SelectTemplate() method.

public class ShapeTemplateSelector : DataTemplateSelector
{
  public DataTemplate Shape1Template { get; set; }
  public DataTemplate Shape2Template { get; set; }
 
  public override DataTemplate SelectTemplate(object item,
    DependencyObject container)
  {
    if (object is MyFirstObject)
      return ShapeTemplate;
    return Shape2Template;
  }
}

I hope this helps.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
Diagram
Asked by
Jason
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or