简体   繁体   中英

DataReader into WPF TextBox vs RichTextBox

When I set the TextBox.Text property equal to DataReader.GetString(x), my data displays correctly.

while (dr.Read())
{
    string data = dr.GetString(2);
    contentTextBox.Text = data;
}

However, I'm trying to use a RichTextBox instead of a TextBox to take advantage of the RichTextBox.Find method, but when I do this it fills the RichTextBox with a new line after each character

while (dr.Read())
{
    string data = dr.GetString(2);
    contentRichTextBox.AppendText(data);
}

How can I get the string data into the RichTextBox with the correct formatting (ie: the same way that it is read into TextBox.Text)?

Thanks!

@jdweng suggested that the RichTextBox was not wide enough. This was definitely one of the issues since its width was set to "Auto." Even though the regular TextBox automatically resizes based on content if it's Width="Auto", RichTextBox does not resize that way.

Update: Was able to get the RichTextBox to resize based on content/window resize by adding a FlowDocument tag with its "PageWidth" bound to the RichTextBox:

<RichTextBox Name="contentTextBox" Height="Auto" Width="Auto">
    <FlowDocument PageWidth="{Binding ElementName=contentTextBox, Path=ActualWidth}" />
</RichTextBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM