Diagram Viewer with tracking changed selection

Back to table of contents


Step by Step:

Diagram Viewer with tracking changed selection

The template of an application shows how you can track selection changes.  

Step 1

Create new project in Microsoft Visual Studio environment. Select item New\Project from the main menu.



Select Silverlight Application, set name of the project – “OnSelectionChanged”, set directory to save the project to.



While creation of the project uncheck option “Host the Silverlight application in a new Web site”.



After that Visual Studio will create a new solution containing one project.

Step 2

Add and set up SharpShooter Diagrams component. It’s necessary to add SharpShooter Diagrams component to the Silverlight application to add the diagrams designer. In order to do that, you should add references to the following assemblies:

Aphalina.Core.dll

Aphalina.Diagrams.Components.dll

Aphalina.Diagrams.DesignTime.dll

Aphalina.Diagrams.DesignTime.UI.dll

Aphalina.Diagrams.dll

Aphalina.Diagrams.FlowCharts.DesignTime.dll

Aphalina.Diagrams.FlowCharts.dll

Aphalina.Diagrams.UI.Controls.dll

Aphalina.Diagrams.UI.Models.dll

Aphalina.Diagrams.UI.Ribbon.dll

Aphalina.Drawing.Connections.DesignTime.dll

Aphalina.Drawing.Connections.dll

Aphalina.Drawing.Core.dll

Aphalina.Drawing.dll

Aphalina.Drawing.UI.Controls.dll

Aphalina.Drawing.UI.Models.dll

Aphalina.Drawing.UI.Ribbon.dll

Aphalina.Framework.dll

In order to do it, right-click References of the OnSelectionChanged in the "Solution Explorer" and select “Add Reference” item in the popup menu.





You should also add references to the following assemblies:

System.Windows.Controls.dll

System.Windows.Controls.Input.dll

System.Windows.Controls.Layout.Toolkit.dll

System.Windows.Controls.Toolkit.dll



Your References should look like this:



Step 3

In order to view and change already created diagrams we need to add DiagramComponent control to the MainPage.xaml. Open it in the designer. Right-click MainPage.xaml and select View Designer in the popup menu.



Add xml namespace for the diagrams.

Then add DiagramComponent and panel which is displaying names of selected elements.

After these steps Main.Page.xaml code should look like this:

<UserControl x:Class="OnSelectionChanged.MainPage"

     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:diagrams="http://schemas.aphalina.com/diagrams"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>

                <ColumnDefinition Width="Auto"/>

            </Grid.ColumnDefinitions>

            <diagrams:DiagramComponent x:Name="diagramComponent" ShowDocumentTabs="False"/>

            <Grid Grid.Column="1" Background="White">

                <StackPanel Margin="5">

                    <TextBlock Margin="5" FontWeight="Bold">Selected elements:</TextBlock>

                    <ItemsControl x:Name="selectionList">

                        <ItemsControl.ItemTemplate>

                            <DataTemplate>

                                <TextBlock Text="{Binding OutlineName}"/>

                            </DataTemplate>

                        </ItemsControl.ItemTemplate>

                    </ItemsControl>

                </StackPanel>

            </Grid>

        </Grid>

    </Grid>

</UserControl>

Please, take a look at the following lines:

<Grid Grid.Column="1" Background="White">

                <StackPanel Margin="5">

                    <TextBlock Margin="5" FontWeight="Bold">Selected elements:</TextBlock>

                    <ItemsControl x:Name="selectionList">

                        <ItemsControl.ItemTemplate>

                            <DataTemplate>

                                <TextBlock Text="{Binding OutlineName}"/>

                            </DataTemplate>

                        </ItemsControl.ItemTemplate>

                    </ItemsControl>

                </StackPanel>

            </Grid>

//in this lines we are forming the list with selected items

Step 4

You need to add ready diagrams in your project. In order to do this right-click on OnSelectionChanged, select Add, then select New Folder and name it Diagrams.



It should look like this:



Then right-click on this folder, select Add -> Existing Item…



Then select .grl files, that you’ve created earlier or .grl files from the samples. Then click on Add. In this example we add FlowChart.grl.


Your Solution Explorer should look like this:



Then open Properties of added diagrams.



Select BuildAction, then Resource.



Repeat these steps with other added diagrams.

Step 5

Open the MainPage.xaml.cs source code and add the following code.

public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)

        {

            using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/FlowChart.grl"))

            {

                diagramComponent.OpenDiagram(stream, "Flow Chart");

            }

            diagramComponent.Model.DocumentModel.SelectionService.SelectionChanged += new EventHandler(SelectionService_SelectionChanged);

        }

        void SelectionService_SelectionChanged(object sender, EventArgs e)

        {

            selectionList.ItemsSource = null;

            selectionList.ItemsSource = diagramComponent.Model.DocumentModel.SelectionService.Selection;

        }

    }

Please, take a look at the following lines:

using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/FlowChart.grl"))

      {

        diagramComponent.OpenDiagram(stream, "Flow Chart");

      }

 //in this lines we load the file from the resources to stream, and then open it

diagramComponent.Model.DocumentModel.SelectionService.SelectionChanged += new EventHandler(SelectionService_SelectionChanged);

//in this lines we add new EventHandler for event selecting

void SelectionService_SelectionChanged(object sender, EventArgs e)

        {

            selectionList.ItemsSource = null;

            selectionList.ItemsSource = diagramComponent.Model.DocumentModel.SelectionService.Selection;

        }

//in this lines we are forming the list for selected items

Step 6

Launch application by clicking the “Start Debugging”  button on the main Visual Studio toolbar. 



The application will open in the browser. You can view already created diagrams and all selected elements in diagrams.


Add Feedback