Skip to content

Commit

Permalink
Merge pull request #65 from SyncfusionExamples/WPF-858390-SamplesForKB
Browse files Browse the repository at this point in the history
WPF-858390-To add the samples for KB
  • Loading branch information
KarkuvelRajan authored Mar 15, 2024
2 parents 24514cc + 9d2f85d commit 36e9293
Show file tree
Hide file tree
Showing 71 changed files with 3,338 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@ This repository contains the samples that demonstrate the functionalities of Dia
<tr>
<td><a href="Samples/Swimlane">Swimlane</a></td>
</tr>
<tr>
<td><a href="Samples/PageBreaks">Swimlane</a></td>
</tr>
<tr>
<td><a href="Samples/RubberBandZoom">Swimlane</a></td>
</tr>
</table>
14 changes: 14 additions & 0 deletions Samples/Connector/UpdateAverageLength/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Update average length sample

This sample demonstrate how to update the length of the connector segments

__*Documentation*__: https://help.syncfusion.com/wpf/diagram/connector/segments/orthogonal

## Project pre-requisites
To run this application, you need to have the below two in your system

* [Visual Studio 2019](https://www.visualstudio.com/wpf-vs)
* [Syncfusion.SfDiagram.WPF](https://www.nuget.org/packages/Syncfusion.SfDiagram.WPF/) nuget package. To install the package using NuGet Package Manager, refer this [link](https://docs.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio#nuget-package-manager).

## Deploying and running the sample
* To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or selectDebug > Start Without Debugging.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UpdateAverageLength", "UpdateAverageLength\UpdateAverageLength.csproj", "{0A8230A5-7EA8-40D7-A6DB-97193B33B81C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0A8230A5-7EA8-40D7-A6DB-97193B33B81C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A8230A5-7EA8-40D7-A6DB-97193B33B81C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A8230A5-7EA8-40D7-A6DB-97193B33B81C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A8230A5-7EA8-40D7-A6DB-97193B33B81C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {565BD1C0-5F70-47E9-916B-C6E4DF5E5927}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="UpdateAverageLength.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UpdateAverageLength"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace UpdateAverageLength
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Window x:Class="UpdateAverageLength.MainWindow"
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:local="clr-namespace:UpdateAverageLength"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<!--Resource Dictionary which contains predefined shapes for Node-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Syncfusion.SfDiagram.Wpf;component/Resources/BasicShapes.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style TargetType="syncfusion:Node">
<Setter Property="ShapeStyle">
<Setter.Value>
<Style TargetType="Path">
<Setter Property="Fill" Value="#FF5B9BD5"/>
<Setter Property="Stretch" Value="Fill"/>
<Setter Property="Stroke" Value="#FFEDF1F6 "/>
</Style>
</Setter.Value>
</Setter>
</Style>

<Style TargetType="syncfusion:Connector">
<Setter Property="ConnectorGeometryStyle">
<Setter.Value>
<Style TargetType="Path">
<Setter Property="Stroke" Value="#6BA5D7"/>
<Setter Property="StrokeThickness" Value="1"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="TargetDecoratorStyle">
<Setter.Value>
<Style TargetType="Path">
<Setter Property="Fill" Value="#6BA5D7"/>
<Setter Property="StrokeThickness" Value="1"/>
</Style>
</Setter.Value>
</Setter>
</Style>

</ResourceDictionary>
</Window.Resources>

<Grid>
<!--Initialize the Sfdiagram-->
<syncfusion:SfDiagram x:Name="diagram">
</syncfusion:SfDiagram>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using Syncfusion.UI.Xaml.Diagram;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UpdateAverageLength
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
(diagram.Info as IGraphInfo).ConnectorSourceChangedEvent += MainWindow_ConnectorSourceChangedEvent;
(diagram.Info as IGraphInfo).ConnectorTargetChangedEvent += MainWindow_ConnectorTargetChangedEvent;
(diagram.Info as IGraphInfo).SelectorChangedEvent += MainWindow_SelectorChangedEvent;

diagram.SnapSettings = new SnapSettings() { SnapConstraints = SnapConstraints.All };
diagram.PortVisibility = PortVisibility.Visible;
diagram.Nodes = new NodeCollection();
diagram.Connectors = new ConnectorCollection();
//Creating nodes
CreateNodes(600, 200);
CreateNodes(500, 400);
CreateNodes(300, 400);
CreateNodes(100, 400);
}
//To update the average segment length for existing connectors.
private void MainWindow_SelectorChangedEvent(object sender, SelectorChangedEventArgs args)
{
if (args.NewValue.InteractionState == NodeChangedInteractionState.Dragged || args.NewValue.InteractionState == NodeChangedInteractionState.Resized
|| args.NewValue.InteractionState == NodeChangedInteractionState.Rotated)
{
var nodes = (diagram.SelectedItems as SelectorViewModel).Nodes as IEnumerable<object>;
foreach (var item in nodes)
{
var node = item as NodeViewModel;
if ((node.Info as INodeInfo).InConnectors != null && (node.Info as INodeInfo).InConnectors.Any())
{
foreach (var connector in (node.Info as INodeInfo).InConnectors as IEnumerable<object>)
{
//updating the segment length for existing connector
UpdateSegmentLength(connector as ConnectorViewModel);
}
}

if ((node.Info as INodeInfo).OutConnectors != null && (node.Info as INodeInfo).OutConnectors.Any())
{
foreach (var connector in (node.Info as INodeInfo).OutConnectors as IEnumerable<object>)
{
//updating the segment length for existing connector
UpdateSegmentLength(connector as ConnectorViewModel);
}
}
}
}
}

private void MainWindow_ConnectorTargetChangedEvent(object sender, ChangeEventArgs<object, ConnectorChangedEventArgs> args)
{
//updating the segment length at connector drawn completed.
if (args.NewValue.DragState == DragState.Completed)
UpdateSegmentLength(args.Item as ConnectorViewModel);
}

private void MainWindow_ConnectorSourceChangedEvent(object sender, ChangeEventArgs<object, ConnectorChangedEventArgs> args)
{
//updating the segment length at connector drawn completed
if (args.NewValue.DragState == DragState.Completed)
UpdateSegmentLength(args.Item as ConnectorViewModel);
}

private void CreateNodes(double offsetX, double offsetY)
{
NodeViewModel node = new NodeViewModel()
{
UnitWidth = 100,
UnitHeight = 100,
OffsetX = offsetX,
OffsetY = offsetY,
Shape = App.Current.MainWindow.Resources["Rectangle"],
//creating ports.
Ports = new PortCollection()
{
new NodePortViewModel()
{
NodeOffsetX = 0.5,
NodeOffsetY = 0,
},
new NodePortViewModel()
{
NodeOffsetX = 0.5,
NodeOffsetY = 1,
},
new NodePortViewModel()
{
NodeOffsetX = 0,
NodeOffsetY = 0.5,
},
new NodePortViewModel()
{
NodeOffsetX = 1,
NodeOffsetY = 0.5,
}
}
};
//adding the node diagram nodes collection
(diagram.Nodes as NodeCollection).Add(node);
}

//Method to update average segment length for connector
private void UpdateSegmentLength(ConnectorViewModel connector)
{
//getting segments points of the connector drawn.
List<Point> segmentsPoints = (connector.Info as IConnectorInfo).ToPoints(true).ToList();
//total segment count of the connector
int segmentsCount = segmentsPoints.Count;
//for non-edited segments
if (segmentsCount == 4)
{
List<Point> newPoints = new List<Point>();
//for top and bottom direction connectors
if (segmentsPoints[0].X == segmentsPoints[1].X)
{
//average y distance need to update for segment
double average = (segmentsPoints[segmentsCount - 1].Y - segmentsPoints[0].Y) / 2;
//new y position for intermediate segments
double newy = segmentsPoints[segmentsCount - 1].Y - average;
//Updating new y point to the intermediate segments
for (int i = 1; i <= segmentsCount - 2; i++)
{
Point pnt = new Point(segmentsPoints[i].X, newy);
newPoints.Add(pnt);
}
}
//for left and right direction connectors.
else if (segmentsPoints[0].Y == segmentsPoints[1].Y)
{
//average x distance need to update for segment
double average = (segmentsPoints[segmentsCount - 1].X - segmentsPoints[0].X) / 2;
//new x position for intermediate segments
double newx = segmentsPoints[segmentsCount - 1].X - average;
//Updating new x point to the intermediate segments
for (int i = 1; i <= segmentsCount - 2; i++)
{
Point pnt = new Point(newx, segmentsPoints[i].Y);
newPoints.Add(pnt);
}
}

//passing new segments points to the connector using LoadSegment method.
(connector.Info as IConnectorInfo).LoadSegments(newPoints);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("UpdateAverageLength")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UpdateAverageLength")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 36e9293

Please sign in to comment.