This question is locked. New answers and comments are not allowed.
I am using MVVM in my project. Everything seems to be fine but when i move the diagram shapes, the connectors does not work properly. I've attached the screenshots of my problem. 1st picture is when drop the shapes into the diagram and the 2nd picture is when i move the shapes. The connectors does not come with the shape.
In the project i created my own Shape(inherits RadDiagramShape) and Link(inherits RadDiagramConnection and ILink<Shape>) and GraphSource(inherits IObservableGraphSource) classes.
This is how my GraphSource class looks like :
private ObservableCollection<Shape> _items = new ObservableCollection<Shape>();
public IEnumerable Items
{
get
{
return _items;
}
}
private ObservableCollection<Link> _links = new ObservableCollection<Link>();
public IEnumerable<ILink> Links
{
get
{
return _links;
}
}
public void AddLink(ILink link)
{
_links.Add((Link) link);
}
public void AddNode(object node)
{
_items.Add((Shape)node);
}
public ILink CreateLink(object source, object target)
{
return new Link() { Source = (Shape)source, Target = (Shape)target };
}
//The problem is in the source shape, not in the target shape.
public object CreateNode(IShape shape)
{
return new Shape() { ShapeType = (shape as Shape).ShapeType };
}
public bool RemoveLink(ILink link)
{
return _links.Remove((Link)link);
}
public bool RemoveNode(object node)
{
return _items.Remove((Shape)node);
}
The xaml file where the diagram and shapes are located :
<Grid Grid.Row="1" Grid.Column="1"
Background="{Binding SelectedColor, ElementName=BackgroundColorEditor, Converter={StaticResource colorToBrushConverter}}">
<telerik:RadDiagram x:Name="diagram" MinWidth="500" Margin="-1 -1 0 0"
GraphSource="{Binding GraphSource}"
ConnectionStyle="{StaticResource ConnectionStyle}"
extensions:DiagramExtensionProperties.ShouldDetachConnections="True"
IsBackgroundSurfaceVisible="{Binding IsGridVisible, Mode=TwoWay}"
IsSnapToGridEnabled="{Binding IsSnapEnabled, Mode=TwoWay}"
primitives:GraphPaper.CellSize="{Binding CellSize}"
primitives:GraphPaper.LineStroke="{Binding SelectedColor, ElementName=GridColorEditor, Converter={StaticResource colorToBrushConverter}}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden" SnapX="{Binding SnapX, Mode=TwoWay}"
SnapY="{Binding SnapY, Mode=TwoWay}" Zoom="{Binding ZoomLevel, Mode=TwoWay}">
<primitives:ItemInformationAdorner.AdditionalContent>
<this:SettingsPane />
</primitives:ItemInformationAdorner.AdditionalContent>
</telerik:RadDiagram>
</Grid>
<extensions:RadDiagramToolbox x:Name="toolbox" HorizontalAlignment="Right" Margin="0 0 -1 -1"
Width="330" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding GalleryItems}"
ItemTemplate="{StaticResource ToolboxTemplate}" Title="Gallery"
Header="{Binding SelectedItem.Header, RelativeSource={RelativeSource Self}}"
Visibility="{Binding IsChecked, ElementName=toolboxButton, Converter={StaticResource BooleanToVisibilityConverter}}" />
And finally the ViewModel :
private GraphSource _graphSource = null;
public GraphSource GraphSource
{
get
{
if (_graphSource == null)
_graphSource = new GraphSource();
return _graphSource;
}
set
{
if (_graphSource != value)
{
_graphSource = value;
RaisePropertyChanged(() => GraphSource);
}
}
}
private IEnumerable _galleryItems = null;
public IEnumerable GalleryItems
{
get
{
if (_galleryItems == null)
_LoadItems();
return _galleryItems;
}
}
private void _LoadItems()
{
HierarchicalGalleryItemsCollection items = new HierarchicalGalleryItemsCollection();
items.Clear();
Gallery galery = new Gallery(){ Header = "Shapes" };
galery.Items.Add(new GalleryItem("My Shape", new Shape() { ShapeType = ShapeType.MyShape }));
items.Add(galery);
_galleryItems = items;
}
In the project i created my own Shape(inherits RadDiagramShape) and Link(inherits RadDiagramConnection and ILink<Shape>) and GraphSource(inherits IObservableGraphSource) classes.
This is how my GraphSource class looks like :
private ObservableCollection<Shape> _items = new ObservableCollection<Shape>();
public IEnumerable Items
{
get
{
return _items;
}
}
private ObservableCollection<Link> _links = new ObservableCollection<Link>();
public IEnumerable<ILink> Links
{
get
{
return _links;
}
}
public void AddLink(ILink link)
{
_links.Add((Link) link);
}
public void AddNode(object node)
{
_items.Add((Shape)node);
}
public ILink CreateLink(object source, object target)
{
return new Link() { Source = (Shape)source, Target = (Shape)target };
}
//The problem is in the source shape, not in the target shape.
public object CreateNode(IShape shape)
{
return new Shape() { ShapeType = (shape as Shape).ShapeType };
}
public bool RemoveLink(ILink link)
{
return _links.Remove((Link)link);
}
public bool RemoveNode(object node)
{
return _items.Remove((Shape)node);
}
The xaml file where the diagram and shapes are located :
<Grid Grid.Row="1" Grid.Column="1"
Background="{Binding SelectedColor, ElementName=BackgroundColorEditor, Converter={StaticResource colorToBrushConverter}}">
<telerik:RadDiagram x:Name="diagram" MinWidth="500" Margin="-1 -1 0 0"
GraphSource="{Binding GraphSource}"
ConnectionStyle="{StaticResource ConnectionStyle}"
extensions:DiagramExtensionProperties.ShouldDetachConnections="True"
IsBackgroundSurfaceVisible="{Binding IsGridVisible, Mode=TwoWay}"
IsSnapToGridEnabled="{Binding IsSnapEnabled, Mode=TwoWay}"
primitives:GraphPaper.CellSize="{Binding CellSize}"
primitives:GraphPaper.LineStroke="{Binding SelectedColor, ElementName=GridColorEditor, Converter={StaticResource colorToBrushConverter}}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden" SnapX="{Binding SnapX, Mode=TwoWay}"
SnapY="{Binding SnapY, Mode=TwoWay}" Zoom="{Binding ZoomLevel, Mode=TwoWay}">
<primitives:ItemInformationAdorner.AdditionalContent>
<this:SettingsPane />
</primitives:ItemInformationAdorner.AdditionalContent>
</telerik:RadDiagram>
</Grid>
<extensions:RadDiagramToolbox x:Name="toolbox" HorizontalAlignment="Right" Margin="0 0 -1 -1"
Width="330" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding GalleryItems}"
ItemTemplate="{StaticResource ToolboxTemplate}" Title="Gallery"
Header="{Binding SelectedItem.Header, RelativeSource={RelativeSource Self}}"
Visibility="{Binding IsChecked, ElementName=toolboxButton, Converter={StaticResource BooleanToVisibilityConverter}}" />
And finally the ViewModel :
private GraphSource _graphSource = null;
public GraphSource GraphSource
{
get
{
if (_graphSource == null)
_graphSource = new GraphSource();
return _graphSource;
}
set
{
if (_graphSource != value)
{
_graphSource = value;
RaisePropertyChanged(() => GraphSource);
}
}
}
private IEnumerable _galleryItems = null;
public IEnumerable GalleryItems
{
get
{
if (_galleryItems == null)
_LoadItems();
return _galleryItems;
}
}
private void _LoadItems()
{
HierarchicalGalleryItemsCollection items = new HierarchicalGalleryItemsCollection();
items.Clear();
Gallery galery = new Gallery(){ Header = "Shapes" };
galery.Items.Add(new GalleryItem("My Shape", new Shape() { ShapeType = ShapeType.MyShape }));
items.Add(galery);
_galleryItems = items;
}