xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.89k stars 877 forks source link

RichTextBox: Remove wordwrap feature #911

Open xceedsoftware opened 7 years ago

xceedsoftware commented 7 years ago

shardaiyer[CodePlex]
Hi,

Is there any way to remove wordwrap feature?

I have wpf application in which there is richtextbox control. I added some multiline text with tabs and spaces into this richtextbox. If I increase the font all my text formatting is change and a vertical scroll bar appears.

I feel this is caused because of word wrap.

Regards, Sharda.

xceedsoftware commented 7 years ago

shardaiyer[CodePlex]
Hi BoucherS,

I tried to read and write using TextRange and StreamReader/ StreamWriter. But still tab issue is exists.

The problem is there in Microsoft RichtextBox. Tabs inserted between the text has no issues. But if Tab is inserted at start of line, then the RichtextBox converts that tab to TextIndent which results in reformatting it seems.

I am really stuck with how to solve this issue. I tried various options like handling the PreviewKeyDown event and replacing tabs with 4 spaces. But this solution is also not working perfectly.

Regards, Sharda

xceedsoftware commented 7 years ago

BoucherS[CodePlex]
Hi shardaiyer,

You are right, XamlFormatter will contains formating tags but doesn’t support images. So RTF formatter should be used is you need images.

After some test, the tab as different spacings depending where it is positionned on a line. It looks like the Default RichTextBox (which wrap by default) may act on tab size.

Alos, If adding 1 tab at beginning of second line, here's what is saved : FlowDocument - Paragraph - Inlines - 3 Runs representing first line, the tab and the other lines. And here's what's loaded : FlowDocument - Paragraph - Inlines - 3 Spans where each contains : a) Inlines - 1 Run representing the first line b) Inlines - 1 Run representing the tab c) Inlines - 1 Run representing the other lines

So the problem may come from the Microsoft RichTextBox Wrap + Tabs or from File.WriteAllText + File.ReadAllText.

xceedsoftware commented 7 years ago

shardaiyer[CodePlex]
Hi BoucherS,

I am asking some RTFFormatter fix because, If I paste any image in RTFTextBox and Formatter is XamlFormatter, then on save and load, the image will not be present.

In case of RTFFormatter, image is still there when we load.

Regards, Sharda.

xceedsoftware commented 7 years ago

shardaiyer[CodePlex]
Hi BoucherS,

If I want to use RTFFormatter, how can I fix this tab issue?

Regards, Sharda.

xceedsoftware commented 7 years ago

BoucherS[CodePlex]
Hi,

The vertical scrollBars appears because you have set : VerticalScrollBarVisibility=Auto.

For the formatting that is different upon loading a save data, what I can see is that using RtfFormatter will have different value for tab spacing. For example, when I do a tab in the RichTextBox, it takes 4 spaces. If I save ans load, the tab now takes 8 spaces....changing the formating.

If you starts the app, do a save and a load, and then add the tab, it will take 8 spaces. Then if you save and load, the formatting will be kept.

If you use the XamlFormatter, doing a tab in the RichTextBox takes 4 spaces. If I save and load, the formating will be kept.

xceedsoftware commented 7 years ago

shardaiyer[CodePlex]
Hi, I have created a sample application. To reproduce the issue please execute below steps:-

Run the application Copy below steps in rich text box. CodePlex is Microsoft's free open source project hosting site. You can create projects to share with the world, collaborate with others on their projects, and download open source software. Entity Framework is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. Select the complete text and increase the font by 24. Vertical scroll will appear. Insert tab in front of every line. Click on save. Click on load. You will observe that formatting is changed.

Below is source code. MainWindow.xaml Window x:Class=TryRichTextBoxWPFToolKit.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525 xmlns:wpftoolkit=http://schemas.xceed.com/wpf/xaml/toolkit Grid Grid.RowDefinitions RowDefinition Height=Auto/ RowDefinition Height=Auto/ RowDefinition Height=Auto/ /Grid.RowDefinitions wpftoolkit:RichTextBox x:Name=PART_RichText Width=400 Height=200 AcceptsTab=True Background=Black BorderThickness=0 CaretBrush=White Foreground=White HorizontalScrollBarVisibility=Auto Text={Binding Path=RtfDocument, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} VerticalScrollBarVisibility=Auto Grid.Row=0 wpftoolkit:RichTextBox.TextFormatter wpftoolkit:RtfFormatter/ /wpftoolkit:RichTextBox.TextFormatter wpftoolkit:RichTextBoxFormatBarManager.FormatBar wpftoolkit:RichTextBoxFormatBar/ /wpftoolkit:RichTextBoxFormatBarManager.FormatBar /wpftoolkit:RichTextBox Button Content=Load Click=OnClickLoad Width=50 Height=50 Grid.Row=1/Button Button Content=Save Click=OnClickSave Width=50 Height=50 Grid.Row=2/Button /Grid /Window

MainWindow.xaml.cs using System; using System.Collections.Generic; using System.IO; 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;

namespace TryRichTextBoxWPFToolKit { /// summary /// Interaction logic for MainWindow.xaml /// /summary public partial class MainWindow : Window { private string _fileName;

    public MainWindow()
    {
        InitializeComponent();
        var documentFolderPath = Directory.GetCurrentDirectory();
        _fileName = Path.Combine(documentFolderPath, temp + _rtf.rtf);
        this.DataContext = new MainWindowViewModel();
    }

    private void OnClickLoad(object sender, RoutedEventArgs e)
    {
        (DataContext as MainWindowViewModel).RtfDocument = string.Empty;
        var lines = File.ReadAllText(_fileName);
        (DataContext as MainWindowViewModel).RtfDocument = lines;
    }

    private void OnClickSave(object sender, RoutedEventArgs e)
    {
        var rtfCode = (DataContext as MainWindowViewModel).RtfDocument;

        //Save RTF code in the rtf file in document folder
        File.WriteAllText(_fileName, rtfCode);
        MessageBox.Show(Save Successful);
        (DataContext as MainWindowViewModel).RtfDocument = string.Empty;
    }
}

}

MainWindowViewModel.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace TryRichTextBoxWPFToolKit { public class MainWindowViewModel : INotifyPropertyChanged { public string _rtfDocument;

    public string RtfDocument
    {
        get
        {
            return _rtfDocument;
        }
        set
        {
            _rtfDocument = value;
            OnPropertyChanged(RtfDocument);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

}

Please let me know your email id. I will send the text application to you. Regards, Sharda.

xceedsoftware commented 7 years ago

BoucherS[CodePlex]
Hi,

The RichTextBox contained in the Xceed.Wpf.Toolkit derives from System.Windows.Controls.RichTextBox. Nothing specific is done about text wrapping.

I can't see where this scrollbar comes from. Can you paste a sample ? Thanks.