【WPF】DiffPlex 文本比对工具

背景

现行的文本编辑器大多都具备文本查询的能力,但是并不能直观的告诉用户两段文字的细微差异,所以对比工具在某种情况下,就起到了很便捷的效率。

关于 DiffPlex

  • DiffPlex 是用于生成文本差异的 C# 库

准备

  • NuGet 包
  • DiffPlex.Wpf 主要包
  • MaterialDesignThemes 主题包

代码实现

  • MainWindow.xaml
<Window
    x:Class="TextComparisonTool.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:diffplex="clr-namespace:DiffPlex.Wpf.Controls;assembly=DiffPlex.Wpf"
    xmlns:local="clr-namespace:TextComparisonTool"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="文本比对工具"
    Width="800"
    Height="450"
    Icon="DiffPlex.ico"
    WindowState="Maximized"
    mc:Ignorable="d">

    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <WrapPanel>
            <Button
                x:Name="BtnInput"
                Click="BtnInput_Click"
                Content="输入文本"
                Style="{DynamicResource MaterialDesignFlatAccentBgButton}" />
        </WrapPanel>
        <diffplex:DiffViewer x:Name="DiffView" Grid.Row="1" />
    </Grid>

</Window>
  • MainWindow.xaml.cs
using System.Windows;

namespace TextComparisonTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();           
        }
         

        private void BtnInput_Click(object sender, RoutedEventArgs e)
        {
            InputOldeTextAndNewText input = new();

            input.ShowDialog();

            if (input.DialogResult is true)
            {
                DiffView.OldText = input.txtOldText.Text;
                DiffView.NewText = input.txtNewText.Text;
            }
        }
    }
}
  • InputOldeTextAndNewText.xaml
<Window
    x:Class="TextComparisonTool.InputOldeTextAndNewText"
    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"
    Title="输入新旧文本"
    Width="850"
    Height="500"
    Icon="DiffPlex.ico"
    ResizeMode="CanMinimize"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">
    <Border Margin="5" CornerRadius="11">
        <StackPanel>
            <TextBlock Style="{DynamicResource MaterialDesignBody1TextBlock}" Text="源文本" />
            <TextBox
                x:Name="txtOldText"
                AcceptsReturn="True"
                MaxLines="10"
                MinLines="10"
                TextWrapping="Wrap" />
            <TextBlock
                VerticalAlignment="Center"
                Style="{DynamicResource MaterialDesignBody1TextBlock}"
                Text="新文本" />
            <TextBox
                x:Name="txtNewText"
                AcceptsReturn="True"
                MaxLines="10"
                MinLines="10"
                TextWrapping="Wrap" />
            <Button
                x:Name="BtnText"
                Margin="10"
                Click="BtnText_Click"
                Content="确认"
                Style="{DynamicResource MaterialDesignFlatButton}" />
        </StackPanel>
    </Border>
</Window>
  • InputOldeTextAndNewText.xaml.cs
using System.Windows;

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

        private void BtnText_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
    }
}

效果图

在这里插入图片描述

在这里插入图片描述

源码下载地址

文本比对工具源码下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑夜中的潜行者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值