C#WPF如何使用按钮将文本从richtexbox传输到另一个richtexbox?

时间:2018-11-13 00:42:59

标签: c# wpf

我是C#的新手,在WPF中传输文本时遇到了一些问题。我希望当我将代码粘贴到第一个richtexbox中时,它将通过使用按钮进行混淆,并在第二个richtexbox中显示,但在第一个rtb中不应更改。

这是我的代码:

private void ButtonClicked2(object sender, RoutedEventArgs e)
    {
        string keyword = "class";
        string newString = obfuscateString;
        TextRange text = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
        TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
        while (current != null)
        {
            string textInRun = current.GetTextInRun(LogicalDirection.Forward);
            if (!string.IsNullOrWhiteSpace(textInRun))
            {
                int index = textInRun.IndexOf(keyword);
                if (index != -1)
                {
                    TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
                    TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
                    TextRange selection = new TextRange(selectionStart, selectionEnd);
                    selection.Text = newString;
                    selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    _richTextBox.Selection.Select(selection.Start, selection.End);
                    _richTextBox.Focus();
                }
            }
            current = current.GetNextContextPosition(LogicalDirection.Forward);
        }
    }

感谢帮助:)

1 个答案:

答案 0 :(得分:0)

在CodeBehind中不会执行此操作,使用Binding很简单。因此,请定义一个ViewModel,在其中将OriginalText和ModifyedText定义为属性,并在单击Button的情况下将其定义为转换命令。

所以这可能像:

public class MyViewModel: INotifyPropertyChanged
{
    private String _originalText = "My Original Text";
    private String _modifyedText = "Place for Obfuscatet Text";

    public String OriginalText
    {
        get => _originalText;
        set
        {
            if (Equals(value, _originalText)) return;
            _originalText = value;
            OnPropertyChanged();
        }
    }

    public String ModifyedText
    {
        get => _modifyedText;
        set
        {
            if (Equals(value, _modifyedText)) return;
            _modifyedText = value;
            OnPropertyChanged();
        }
    }

    public ICommand Obfuscate { get; set; }

    public GuestViewModel()
    {
        Obfuscate = new RelayCommand(DoObfuscate, o => true);
    }

    private void DoObfuscate(object obj)
    {
        ModifyedText = DoModify(OriginalText);
    }

    //Implementation of INotifyPropertyChanged and DoModify etc...
}

现在将String属性绑定到两个RichTextBox(有多个选项可以执行此操作,请参见Richtextbox wpf binding)和Button到Command。

Xaml可能像:

<Grid DataContext="{Binding TODO}">
    <Grid.RowDefinitions>
        <RowDefinition Height="150"></RowDefinition>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="150"></RowDefinition>
    </Grid.RowDefinitions>
    <RichTextBox Grid.Row="0">
         <FlowDocument PageHeight="180">
             <Paragraph>
                 <Run Text="{Binding OriginalText, Mode=TwoWay}"/>
             </Paragraph>
         </FlowDocument>
     </RichTextBox>
     <Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
     <RichTextBox Grid.Row="2">
         <FlowDocument PageHeight="180">
             <Paragraph>
                 <Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
             </Paragraph>
         </FlowDocument>
     </RichTextBox>
 </Grid>

有关绑定的更多信息,请参见:https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/

相关问题