syncfusion-diagram:demo1如何实现

news2025/2/24 8:14:32

 xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
 xmlns:stencil="clr-namespace:Syncfusion.UI.Xaml.Diagram.Stencil;assembly=Syncfusion.SfDiagram.WPF"当我们进入syncfusion的diagram中,可以看到,一个非常炫酷的例子

不仅实现了上方的工具栏同时还有下方的模板栏和画布。

如何实现相同的效果呢

1.注册密钥

syncfusion在使用时需要密钥,首先在visual studio里下生成一个新的wpf工程,加入下面这六个nuget包,看名字就知道第一个是许可证相关的,没有这个你就没法声明许可证密钥。

 ,然后现在,在mainwindow.xaml.cs文件中修改代码。

1.加入

using Syncfusion.UI.Xaml.Diagram;//引用
using Syncfusion.UI.Xaml.Diagram.Layout;//布局管理功能引用
using Syncfusion.UI.Xaml.Diagram.Stencil;//符号库引用

2.在初始化之前加入Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("你申请的许可证密钥");一定要在初始化之前加入。

关于密钥的申请,这篇文章很清楚:

安装和使用_syncfusion的密钥-CSDN博客

using System.Text;
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;
using Syncfusion.UI.Xaml.Diagram;//引用
using Syncfusion.UI.Xaml.Diagram.Layout;//布局管理功能引用
using Syncfusion.UI.Xaml.Diagram.Stencil;//符号库引用

namespace syncfusion_24_4_9_demo1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("你申请的许可证密钥");
            InitializeComponent();
        }
    }
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

接下来就能愉快的进行syncfusion的使用了

2.在mainwindow.xaml 的窗口定义部分添加两行代码

 xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
 xmlns:stencil="clr-namespace:Syncfusion.UI.Xaml.Diagram.Stencil;assembly=Syncfusion.SfDiagram.WPF"

  1. xmlns:syncfusion:引入了 syncfusion 命名空间,允许在XAML中使用Syncfusion控件库。
  2. xmlns:stencil:引入了 stencil 命名空间,用于在XAML中使用Stencil相关的功能。

效果如下面所示

<Window x:Class="syncfusion_24_4_9_demo1.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:syncfusion_24_4_9_demo1"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        xmlns:stencil="clr-namespace:Syncfusion.UI.Xaml.Diagram.Stencil;assembly=Syncfusion.SfDiagram.WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

3.定义窗口的资源

在窗口的资源中定义一个资源字典,先把basicshapes.xaml这个资源字典加入当前资源字典,然后定义node的样式,connector的样式,symbol的样式,然后定义了一个数据模板,用来统一标题。

注:style-样式,只是统一控件的某些属性,templete-模板,则是相当于在原有控件基础上改变内部结构和行为。

<Window.Resources>
    <!--定义资源字典和样式-->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!--初始化形状-->
            <ResourceDictionary Source="/Syncfusion.SfDiagram.Wpf;component/Resources/BasicShapes.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="syncfusion:Node">
            <!--定义目标类型为node的风格-->
            <Setter Property="ShapeStyle">
                <!--内容模板-->
                <Setter.Value>
                    <Style TargetType="Path">
                        <Setter Property="Fill" Value="Blue"></Setter>
                        <Setter Property="Stretch" Value="Fill"></Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

        <!--Style for Connector-->
        <Style TargetType="syncfusion:Connector">
            <!--定义connector的风格-->
            <Setter Property="ConnectorGeometryStyle">
                <!--形状定义-->
                <Setter.Value>
                    <Style TargetType="Path">
                        <Setter Property="Stroke" Value="Black"  />
                        <!--stroke是轮廓-->
                        <Setter Property="StrokeThickness" Value="1" />
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="TargetDecoratorStyle">
                <!--目标描述定义-->
                <Setter.Value>
                    <Style TargetType="Path">
                        <!--路径-->
                        <Setter Property="Stroke" Value="#4f4f4f"  />
                        <Setter Property="Stretch" Value="Fill" />
                        <Setter Property="Fill" Value="#4f4f4f"  />
                        <Setter Property="StrokeThickness" Value="1" />
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

        <!--Symbol类型定义-->
        <Style TargetType="stencil:Symbol">
            <Setter Property="Width" Value="50"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="Padding" Value="3" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="Margin" Value="4"></Setter>
        </Style>
        <DataTemplate x:Key="TitleTemplate">
            <TextBlock x:Name="HeaderText" Text="{Binding}" FontSize="15" FontWeight="SemiBold"  Foreground="#2b579a" />
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>

 4.定义页面

首先把页面划分成两行,第一行放工具栏,这个直接把sfdiagramribbon拖进来就好了,放在第一行,第二行再分成两列,第一列设置成自动宽度,第二列设置成*,因为左边的模板栏可以收缩到页面最左边,所以要设置成auto,这样缩进的时候占据的空间也会相应的收缩,右边画布设置成*,就是要在左边模板栏收缩的时候,右边也能自动占据空间。

 <Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="*"/>
     </Grid.RowDefinitions>
     <syncfusion:SfDiagramRibbon x:Name="DiagramRibbon" Grid.Row="0" DataContext="{Binding ElementName=Diagram}"/>


     <Grid Grid.Row="1">
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="auto"/>
             <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>
         <Grid Grid.Column="0">
             <Grid Background="White">
                 <stencil:Stencil x:Name="stencil"
                          Grid.Column="0"
                          Grid.Row="1"
                          ExpandMode="ZeroOrMore"
                          BorderBrush="#dfdfdf"
                          BorderThickness="1" Title="Shapes" TitleTemplate="{StaticResource TitleTemplate}">
                     <stencil:Stencil.Resources>
                         <ResourceDictionary>
                             <ResourceDictionary.MergedDictionaries>
                                 <!--初始化形状-->
                                 <ResourceDictionary Source="/Syncfusion.SfDiagram.Wpf;component/Resources/BasicShapes.xaml" />
                             </ResourceDictionary.MergedDictionaries>
                         </ResourceDictionary>
                     </stencil:Stencil.Resources>
                     <stencil:Stencil.SymbolSource>
                         <syncfusion:SymbolCollection>
                             <!--Define the DiagramElement- Node-->
                             <syncfusion:NodeViewModel x:Name="node"
                                               Key="Nodes"
                                               UnitHeight="70"
                                               UnitWidth="100"
                                               OffsetX="100"
                                               OffsetY="100"
                                               Shape="{StaticResource Rectangle}">
                             </syncfusion:NodeViewModel>
                             <!--Define the DiagramElement- Connector-->
                             <syncfusion:ConnectorViewModel SourcePoint="100,100"
                                                    Key="Connectors"
                                                    TargetPoint="200,200" />
                             <!--Define the DiagramElement- Group-->
                             <syncfusion:GroupViewModel Key="Groups">
                                 <!--Creates the Groupable Nodes-->
                                 <syncfusion:GroupViewModel.Nodes>
                                     <syncfusion:NodeCollection>
                                         <syncfusion:NodeViewModel UnitHeight="70"
                                                           ID="srcnode"
                                                           OffsetX="0"
                                                           OffsetY="300"
                                                           UnitWidth="100"
                                                           Shape="{StaticResource Rectangle}">
                                         </syncfusion:NodeViewModel>
                                         <syncfusion:NodeViewModel UnitHeight="70"
                                                           ID="tarnode"
                                                           OffsetX="100"
                                                           OffsetY="500"
                                                           UnitWidth="100"
                                                           Shape="{StaticResource Rectangle}">
                                         </syncfusion:NodeViewModel>
                                     </syncfusion:NodeCollection>
                                 </syncfusion:GroupViewModel.Nodes>
                                 <!--Creates the Groupable Connectors-->
                                 <syncfusion:GroupViewModel.Connectors>
                                     <syncfusion:ConnectorCollection>
                                         <syncfusion:ConnectorViewModel SourceNodeID="srcnode"
                                                                TargetNodeID="tarnode" />
                                     </syncfusion:ConnectorCollection>
                                 </syncfusion:GroupViewModel.Connectors>
                             </syncfusion:GroupViewModel>
                         </syncfusion:SymbolCollection>
                     </stencil:Stencil.SymbolSource>

                     <stencil:Stencil.Categories>
                         <stencil:StencilCategoryCollection>
                             <!--Specify the basic shapes category with title and resource key-->
                             <stencil:StencilCategory Title="Basic Shapes" Keys="{StaticResource BasicShapes}"/>
                         </stencil:StencilCategoryCollection>
                     </stencil:Stencil.Categories>
                     
                     <stencil:Stencil.SymbolGroups>
                         <stencil:SymbolGroups>
                             <!--Separate groups based on the key-->
                             <stencil:SymbolGroupProvider MappingName="Key" />
                         </stencil:SymbolGroups>
                     </stencil:Stencil.SymbolGroups>
                     
                 </stencil:Stencil>
             </Grid>
         </Grid>
         <syncfusion:SfDiagram x:Name="Diagram" Constraints="Undoable,Default" Grid.Column="1">
             <syncfusion:SfDiagram.Theme>
                 <syncfusion:OfficeTheme/>
             </syncfusion:SfDiagram.Theme>
             <syncfusion:SfDiagram.Nodes>
                 <syncfusion:NodeCollection/>
             </syncfusion:SfDiagram.Nodes>
             <syncfusion:SfDiagram.Connectors>
                 <syncfusion:ConnectorCollection/>
             </syncfusion:SfDiagram.Connectors>
             <syncfusion:SfDiagram.Groups>
                 <syncfusion:GroupCollection/>
             </syncfusion:SfDiagram.Groups>
             <syncfusion:SfDiagram.SnapSettings>
                 <syncfusion:SnapSettings SnapConstraints="All"/>
             </syncfusion:SfDiagram.SnapSettings>
             <syncfusion:SfDiagram.HorizontalRuler>
                 <syncfusion:Ruler Orientation="Horizontal"/>
             </syncfusion:SfDiagram.HorizontalRuler>
             <syncfusion:SfDiagram.VerticalRuler>
                 <syncfusion:Ruler Orientation="Vertical"/>
             </syncfusion:SfDiagram.VerticalRuler>
         </syncfusion:SfDiagram>
     </Grid>
 </Grid>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1585998.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【2024.4.11练习】国际象棋

题目描述 题目思路 棋盘类问题是一类典型的状态压缩dp问题&#xff0c;将0设为不摆放象棋&#xff0c;1设为摆放象棋。这样棋盘的每一列都可以变成01的序列。每一列有8个格子&#xff0c;所以每列总共有种摆放情况。为了完成递推&#xff0c;需要写出以下功能的预处理函数 ini…

无缝集成:使用Spring Boot和Vue实现头像上传与回显功能

&#x1f31f; 前言 欢迎来到我的技术小宇宙&#xff01;&#x1f30c; 这里不仅是我记录技术点滴的后花园&#xff0c;也是我分享学习心得和项目经验的乐园。&#x1f4da; 无论你是技术小白还是资深大牛&#xff0c;这里总有一些内容能触动你的好奇心。&#x1f50d; &#x…

STC89C52学习笔记(五)

STC89C52学习笔记&#xff08;五&#xff09; 综述&#xff1a;文本讲述了代码中速写模板的创建、如何将矩阵键盘的按键与数字一一对应以及如何创建一个矩阵键盘密码锁。 一、速写模板 点击“templates”&#xff0c;再鼠标右键选择配置&#xff0c;按照以下方式即可修改一些…

正压自动放水器

劣质产品或许能骗一个人 却骗不了一群人 更骗不了五湖四海的人 因为品质&#xff0c;所以传播因为认同&#xff0c;所以分享 一、正压放水器概述&#xff1a; 正压自动放水器的型号为CWG-ZY&#xff0c;C指瓦斯抽放&#xff08;采&#xff09;中抽放&#xff08;采&#xff…

Linux系统概述与安装

Linux的介绍 Linux内核 Linux内核是 Linux 操作系统主要组件&#xff0c;也是计算机硬件与其软件之间的交互入口。它负责两者之间的通信&#xff0c;还要尽可能高效地管理资源 Linux Shell shell是系统的用户界面&#xff0c;提供了用户与内核进行交互操作的一种接口 Linux文…

Vmware虚拟机Centos7固定IP地址

1、点击编辑-虚拟网络编辑器 2、点击更改设置、修改虚拟网络配置器并确认保存&#xff08;见图&#xff09; 这个子网IP和子网掩码的前三位需要一样网关的前三位需要和子网ip一致。 3、打开设置“网络和Internet”&#xff0c;点击“更改适配器选项”&#xff0c;点击适配器VM…

Github第一Star数的国产免费开源防火墙--雷池社区版初步体验

前言 近期准备搭建一个博客网站&#xff0c;用来存储工作室同学们的学习笔记。服务器准备直接放在公网上&#xff0c;方便大家随时随地的上传和浏览&#xff0c;为了防止网站被人日穿成为肉鸡&#xff0c;一些防御措施还是要部署的。 首先明确自己的需求&#xff1a; 零成本…

【数据结构】双向链表 C++

一、什么是双向链表 1、定义 双向链表也叫双链表&#xff0c;是链表的一种&#xff0c;它的每个数据结点中都有两个指针&#xff0c;分别指向直接后继和直接前驱。所以&#xff0c;从双向链表中的任意一个结点开始&#xff0c;都可以很方便地访问它的前驱结点和后继结点。 双…

企业工商信息查询API接口有哪些

当今社会我们几乎每天都在和一些企业打交道&#xff0c;有时候需要确认下这家企业经营范围&#xff0c;注册地址等信息&#xff0c;那怎么办呢&#xff0c;这个时候就需要一些企业工商信息查询的API接口了。 有的时候你可以只知道这家公司的大概企业名称&#xff0c;比如数脉&…

Python中sort()函数、sorted()函数的用法深入讲解(具体实例:蓝桥杯数位排序)

前置知识&#xff1a; 可迭代对象的定义&#xff1a;可迭代对象是指可以被迭代或遍历的对象&#xff0c;即可以使用循环结构对其进行逐个访问的对象。 在Python中常见的可迭代对象有&#xff1a;列表(list)、元组&#xff08;tuple&#xff09;、字符串&#xff08;sting&…

【踩坑日记】Pop!OS中文输入法的坑

文章目录 前言一、编译安装最新的IBus-pinyin输入法1.卸载旧输入法2.安装编译依赖3.下载源码4.编译和安装libpinyin5.编译和安装ibus-libpinyin6.重启IBus服务二、安装Fcitx5前言 使用Linux时,特别是涉及到中文的时候,会遇到一些问题。我最近在使用Pop!OS 22.04,这是Ubuntu…

如何监控容器或K8s中的OpenSearch

概述 当前 OpenSearch 使用的越来越多, 但是 OpenSearch 生态还不尽完善. 针对如下情况: 监控容器化或运行在 K8s 中的 OpenSearch 我查了下, 官方还没有提供完备的方案. 这里如何监控 K8s 中的 OpenSearch, 包括安装 exporter 插件、采集、展示全环节。 OpenSearch 简介…

数据库数据恢复—Sql Server数据库文件丢失如何恢复数据?

服务器数据恢复环境&#xff1a; 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5&#xff0c;划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检&#xff1a; 由于未知原因&#xf…

git 删除本地分支 删除远程仓库分支

语法&#xff1a; 删除本地分支 git branch -D <分支名>删除远程分支 git push <remote名称> <分支名> --delete 示例&#xff1a; 删除本地分支 git branch -D feature/test_listview删除远程分支 git push origin feature/test_listview --delete 两个…

润色问题解惑

上博士为了毕业写学术论文头都大了&#xff0c;但更难受的是英语不咋地&#xff0c;投稿后经常会因为语言问题而惨遭拒稿&#xff0c;每每想起就令人心情郁郁&#xff0c;天台可期。有些审稿人也会直接告知需要专业的修改&#xff0c;那咋整呢&#xff0c;让润色呗&#xff0c;…

腾讯社交广告推广如何开户和费用攻略

腾讯社交广告平台&#xff0c;依托腾讯系庞大的用户基数与丰富的应用场景&#xff0c;为广告主提供了极具潜力的营销渠道。从微信朋友圈、QQ空间到腾讯新闻、腾讯视频等多款热门应用&#xff0c;腾讯社交广告覆盖了亿万级用户群体&#xff0c;是企业提升品牌知名度、推动产品销…

如何做好谷歌广告投放?谷歌广告投放要点解析

市场是在不断变化的&#xff0c;搜索引擎上的网站排名也随着市场的变化而变化。如果你的广告战术一成不变&#xff0c;很容易花冤枉钱。从本质上来讲&#xff0c;谷歌广告的优化工作就是让商家在搜索引擎上保持长久的市场竞争力。 如果商家不经常优化Google广告&#xff0c;可能…

谈谈系列之OA又见OA

确实没想到&#xff0c;绕了一圈&#xff0c;居然又回到了OA&#xff0c;当年从HW从来&#xff0c;就是不想仅仅只做给内部人用的产品&#xff0c;没想到兜兜转转&#xff0c;又回到了给“内部人做产品”的“老路”。 当然&#xff0c;就像先哲赫拉克利特说的——人不能两次走进…

【随笔】Git 高级篇 -- 提交的技巧(下) cherry-pick commit --amend(十九)

&#x1f48c; 所属专栏&#xff1a;【Git】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &#x1f496; 欢迎大…

【CSS】CSS三大特性、盒子模型

目录 CSS三大特性 1、层叠性 2、继承性 3、优先级 盒子模型 1、网页布局的本质 2、盒子模型&#xff08;Box Model&#xff09;组成 3、边框&#xff08;border&#xff09; 3.1、边框的使用 3.2、表格的细线边框 3.3、边框会影响盒子实际大小 4、内边距&#xff0…