【问题标题】:Word matching using external WPF window使用外部 WPF 窗口进行单词匹配
【发布时间】:2020-08-21 09:06:23
【问题描述】:

我有两个 WPF 窗口。

  1. MainWindow,其中包含一个名为 page1RichTextBox 和一个名为 btnFindReplace 的按钮,用于打开 FindReplace 窗口。

page1

  1. FindReplace 窗口,带有两个 TextBoxes 和一个名为 btnConfirm 的按钮。

第一个文本框被称为“txtBoxFind

Find

第二个叫做“txtBoxReplaceWith”

Replace & ReplaceWith

/

/

/

我有几个问题:

  • 当调用 btnConfirm Click 事件时,新的 MainWindow 实例打开。怎么会?
  • 正则表达式匹配总是错误的 - 是因为打开了一个新实例并且它匹配 txtBoxFind.Textno text,因为 page1 的文本 在新实例中是 吗?如果是这样,如何解决?如果不是,为什么总是假的?

/

/

/

MainWindow.xaml.cs

btnFindReplace 点击事件

    private void FindReplace(object sender, RoutedEventArgs e)
    {
        FindReplace findReplaceWindow = new FindReplace();

        findReplaceWindow.Show();
    }

FindReplace.xaml.cs

btnConfirm 点击事件

    private void FindOrReplace(object sender, RoutedEventArgs e)
    {
        MainWindow windowMain = new MainWindow();
        TextRange textRange = new TextRange(windowMain.page1.Document.ContentStart, windowMain.page1.Document.ContentEnd);

        switch (((Button)sender).Content)
        {
            case "Find":
                //Regex.Match(textRange.Text, @"\b" + txtBoxReplaceWith.Text + @"\b");
                
                if (Regex.IsMatch(textRange.Text, @"\b" + txtBoxFind.Text + @"\b"))
                {
                    MessageBox.Show("found");
                }
                else
                {
                    MessageBox.Show("not found");
                }
                break;

            case "Replace":
                //textRange.Text.Replace(txtBoxFind.Text, txtBoxReplace.Text);
                break;
        }
    }

【问题讨论】:

  • 创建一个新的 MainWindow 显然是错误的。你为什么这样做?您应该在显示之前将文本从当前 MainWindow 传递到 FindReplace 窗口。
  • 我是第一次这样做。我创建了一个新的 MainWindow 来访问“page1”富文本框。如何在不创建新 MainWindow 的情况下访问 page1?
  • 如前所述,通过 在显示之前将文本从当前 MainWindow 传递到 FindReplace 窗口,例如作为构造函数参数:new FindReplace(new TextRange(page1.Document.ContentStart, page1.Document.ContentEnd));

标签: c# regex wpf


【解决方案1】:

遵循Clemens 建议并将page1 的文本从当前MainWindow 传递到FindReplace 窗口,然后才实际显示它。

    private void FindReplace(object sender, RoutedEventArgs e)
    {
        FindReplace findReplaceWindow = new FindReplace(new TextRange(page1.Document.ContentStart, page1.Document.ContentEnd));

        findReplaceWindow.Show();
    }

然后创建一个公共的 TextRange 变量并将 textRange 保存在其中。

    public TextRange range;

    public FindReplace(TextRange textRange)
    {
        InitializeComponent();
        range = textRange;
    }

最后,我重新编写了 FindOrReplace 事件(TO-DO 替换功能),一切正常 很好。

    private void FindOrReplace(object sender, RoutedEventArgs e)
    {
        if (((Button)sender).Content == "Find")
        {
            if (Regex.IsMatch(range.Text, @"\b" + txtBoxFind.Text + @"\b"))
            {
                MessageBox.Show("found");
            }
            else
            {
                MessageBox.Show("not found");
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2018-01-20
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多