.net core+网络拓扑图的WPF开源项目-NodeNetwork 二

在上一篇已经在vscode的终端成功创建了.net core项目,并安装NodeNetwork包,且应用到项目上

这次我们继续

将MainWindow.xaml改成如下

<Window x:Class="MyUml.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:nodenetwork="clr-namespace:NodeNetwork.Views;assembly=NodeNetwork"
        xmlns:local="clr-namespace:MyUml"
        mc:Ignorable="d"
        Title="testUml数据库设计v1.0" Height="450" Width="800">
    <Grid>
        <DockPanel>

            <!--DockPanel停靠顶部-->
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="_文件">
                    <MenuItem Header="_新建" />
                    <MenuItem Header="_打开" />
                    <MenuItem Header="_保存" />
                    <Separator />
                    <MenuItem Header="_退出" />
                </MenuItem>
                <MenuItem Header="_工具">
                    <MenuItem Header="_设置" Click="menuSet" />
                </MenuItem>
            </Menu>

            <!--DockPanel停靠底部-->
            <StatusBar DockPanel.Dock="Bottom">
                <StatusBarItem>
                    <TextBlock Text="加载中…" />
                </StatusBarItem>
                <Separator />
                <StatusBarItem>
                    <ProgressBar Value="50" Width="100" />
                </StatusBarItem>
            </StatusBar>

            <!--DockPanel停靠中部-->
            <Grid>
                <!--可移动的布局GridSplitter,中部分成5列-->
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="120" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="120" />
                </Grid.ColumnDefinitions>
                <TreeView x:Name="treeView" >
                    <TreeView.ContextMenu>
                        <ContextMenu>
                            <MenuItem x:Name="ContextMenu1" Header="添加"  Click="cMenu_add"/>
                            <MenuItem x:Name="ContextMenu2" Header="修改"  Click="cMenu_update"/>
                            <MenuItem x:Name="ContextMenu3" Header="删除"  Click="cMenu_del"/>
                        </ContextMenu>
                    </TreeView.ContextMenu>
                </TreeView>
                <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
                <nodenetwork:NetworkView x:Name="networkView" Grid.Column="2" />
                <!-- <Views:NetworkView x:Name="networkView" Grid.Column="2"/> -->
                <GridSplitter Grid.Column="3" Width="5" HorizontalAlignment="Stretch" />
                <Canvas Grid.Column="4" />
            </Grid>

        </DockPanel>
    </Grid>
</Window>

MainWindow.xaml.cs文件改成如下

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;
using DynamicData;
using NodeNetwork.ViewModels;
using NodeNetwork.Views;

namespace MyUml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        // 创建NetworkView视图的ViewModel实例
        private readonly NetworkViewModel _network;

        public MainWindow()
        {
            InitializeComponent();

            _network = new NetworkViewModel();
            networkView.ViewModel = _network;
        }


        private void menuSet(object sender, RoutedEventArgs e)
        {
            // Set aSet = new Set();
            // aSet.ShowDialog();
        }

        private void cMenu_add(object sender, RoutedEventArgs e)
        {
            nodeAdd aNodeAdd = new nodeAdd();
            if (aNodeAdd.ShowDialog() == true)
            {
                this.treeView.Items.Add(aNodeAdd.nameStr);

                // 创建第一个节点ViewModel,设置它的名称并将此节点加入到network
                var node1 = new NodeViewModel();
                node1.Name = aNodeAdd.nameStr;
                node1.Position = new Point(100, 100);
                _network.Nodes.Add(node1);

                // 创建第一个节点的输入端口ViewModel,设置它的名称并加入第一个节点
                var node1Input = new NodeInputViewModel();
                node1Input.Name = "节点1输入";
                node1.Inputs.Add(node1Input);

                var node2Output = new NodeOutputViewModel();
                node2Output.Name = "节点2输出";
                node1.Outputs.Add(node2Output);

                //MessageBox.Show("提示","提示",MessageBoxButton.YesNo)
            }
        }

        private void cMenu_update(object sender, RoutedEventArgs e)
        {
           
            _network.Nodes.Clear();

            int maxX = 10;
            int maxY = 10;
            for (int x = 0; x < maxX; x++)
            {
                for (int y = 0; y < maxY; y++)
                {
                    NodeViewModel node = new NodeViewModel();
                    node.Position = new Point(x * 200, y * 200);
                    _network.Nodes.Add(node);
                    //Debug.WriteLine($"Added node {(x*maxY)+y}");
                }
            }
        }

        private void cMenu_del(object sender, RoutedEventArgs e)
        {
            // Set aSet = new Set();
            // aSet.ShowDialog();
        }

    }
}

之后发现界面多了顶部菜单栏,底部状态栏,中间分三列可移动,如下图:

 在左边树形结构我们可以鼠标右键来动态添加节点,并在右边试图中同步创建可以移动对象

nodeAdd.xaml代码如下

<Window x:Class="MyUml.nodeAdd"
        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:MyUml"
        mc:Ignorable="d"
        Title="添加节点" Height="163.024" Width="284.034">
    <Grid>
        <Button x:Name="button" Content="添加" HorizontalAlignment="Left" Margin="182,95,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.58,2.451"  Click="btn_Ok"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="93,54,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="label" Content="名称:" HorizontalAlignment="Left" Margin="42,52,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>

nodeAdd.xaml.cs代码如下:

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.Shapes;

namespace MyUml
{
    /// <summary>
    /// nodeAdd.xaml 的交互逻辑
    /// </summary>
    public partial class nodeAdd : Window
    {
        public nodeAdd()
        {
            InitializeComponent();
        }

        public string nameStr { get; set; }

        private void btn_Ok(object sender, RoutedEventArgs e)
        {
            this.nameStr = this.textBox.Text;
            this.DialogResult = true;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boyxgb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值