Best way to show huge text in WPF? - wpf

I need to show a really huge amount of text data in WPF code. First i tried to use TextBox (and of course it was too slow in rendering). Now i'm using FlowDocument--and its awesome--but recently i have had another request: text shouldnt be hyphenated. Supposedly it is not (document.IsHyphenationEnabled = false) but i still don't see my precious horizontal scroll bar. if i magnify scale text is ... hyphenated.
public string TextToShow
{
set
{
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FlowDocument document = new FlowDocument(paragraph);
document.IsHyphenationEnabled = false;
flowReader.Document = document;
flowReader.IsScrollViewEnabled = true;
flowReader.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
flowReader.IsPrintEnabled = true;
flowReader.IsPageViewEnabled = false;
flowReader.IsTwoPageViewEnabled = false;
}
}
That's how i create FlowDocument - and here comes part of my WPF control:
<FlowDocumentReader Name="flowReader" Margin="2 2 2 2" Grid.Row="0" />
Nothing criminal =))
I'd like to know how to tame this beast - googled nothing helpful. Or you have some alternative way to show megabytes of text, or textbox have some virtualization features which i need just to enable. Anyway i'll be happy to hear your response!

It's really wrapping not hyphenation. And one can overcome this by setting FlowDocument.PageWidth to reasonable value, the only question was how to determine this value.
Omer suggested this recipe msdn.itags.org/visual-studio/36912/ but i dont like using TextBlock as an measuring instrument for text. Much better way:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FormattedText text = new FormattedText(value, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(paragraph.FontFamily, paragraph.FontStyle, paragraph.FontWeight, paragraph.FontStretch), paragraph.FontSize, Brushes.Black );
FlowDocument document = new FlowDocument(paragraph);
document.PageWidth = text.Width*1.5;
document.IsHyphenationEnabled = false;
Omer - thanks for the direction.

Related

codenameone Picker Alternative to ComboBox

I am getting my feet wet with Codename One. I have looked into more other options like Xamarin, PhoneGap, Ionic for cross platform but I kinda got hooked with Codename one as it really code once and run anywhere.
I've been going through ui elements and I am kinda blocked on populating a combobox (Alternative is Picker)
Let's say I have stores as value pair (storeId, storeName). I want to display the storeName in Picker but keep storeId as the value reference.
Once the store is selected I would like to pass the storeId to an API call.
Is this possible. This might be very simple question but seems bit difficult to implement (I am really new to mobile).
Thank you.
Our recommendation is to avoid ComboBox. It's a UI pattern that doesn't exist on iOS natively and would feel alien on modern phones. It exists in Codename One.
In this code from the sample above you can get a similar effect to a complex multi-field combo box:
Form hi = new Form("Button", BoxLayout.y());
String[] characters = { "Tyrion Lannister", "Jaime Lannister", "Cersei Lannister"};
String[] actors = { "Peter Dinklage", "Nikolaj Coster-Waldau", "Lena Headey"};
int size = Display.getInstance().convertToPixels(7);
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(size, size, 0xffcccccc), true);
Image[] pictures = {
URLImage.createToStorage(placeholder, "tyrion","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/tyrion-lannister-512x512.jpg"),
URLImage.createToStorage(placeholder, "jaime","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/jamie-lannister-512x512.jpg"),
URLImage.createToStorage(placeholder, "cersei","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/cersei-lannister-512x512.jpg")
};
MultiButton b = new MultiButton("Pick A Lanister...");
b.addActionListener(e -> {
Dialog d = new Dialog();
d.setLayout(BoxLayout.y());
d.getContentPane().setScrollableY(true);
for(int iter = 0 ; iter < characters.length ; iter++) {
MultiButton mb = new MultiButton(characters[iter]);
mb.setTextLine2(actors[iter]);
mb.setIcon(pictures[iter]);
d.add(mb);
mb.addActionListener(ee -> {
b.setTextLine1(mb.getTextLine1());
b.setTextLine2(mb.getTextLine2());
b.setIcon(mb.getIcon());
d.dispose();
b.revalidate();
});
}
d.showPopupDialog(b);
});
hi.add(b);
hi.show();
If you insist on using a ComboBox you can use a model to give it any object data you want. Then create a cell render to display the data. This is all discussed in depth in the component section of Codname One's developer guide. Notice that since ComboBox derives from List a lot of the List tips and docs apply to ComboBox.

WPF printing - PrintableAreaWidth & Height stuck in landscape?

I'm printing a FlowDocument using the following code:-
var printDialog = new PrintDialog();
var result = printDialog.ShowDialog();
if (!result.Value)
{
return;
}
var pageWidth = printDialog.PrintableAreaWidth;
var pageHeight = printDialog.PrintableAreaHeight;
flowDoc.ColumnWidth = pageWidth;
flowDoc.PageWidth = pageWidth;
flowDoc.PageHeight = pageHeight;
var paginator = ((IDocumentPaginatorSource)flowDoc).DocumentPaginator;
printDialog.PrintDocument(paginator, "Name");
The print dialog shows the page as being A4 Portrait, however the values of printDialog.PrintableAreaWidth and .PrintableAreaHeight are 1122 & 793 respectively, i.e. landscape.
Changing the orientation or the paper size via the dialog has no effect on these values. What's going on?
Update
I've added a screenshot showing the PrintDialog properties. Notice how the PrintTicket property reflects the correct page size and orientation, yet the two PrintableArea... properties are the wrong way around.
I'm starting to think this is a "funny" with the printer/driver. I've tried printing to the "XPS Document Writer" printer, and the pages render correctly when I view the created file. (And if I view the PrintDialog's properties, the PrintableArea... properties correctly reflect an A4 portrait page).

Printing text in Silverlight that measures larger than page

I have a silverlight application that allows people to enter into a notes field which can be printed, the code used to do this is:
PrintDocument pd = new PrintDocument();
Viewbox box = new Viewbox();
TextBlock txt = new TextBlock();
txt.TextWrapping = TextWrapping.Wrap;
Paragraph pg = new Paragraph();
Run run = new Run();
pg = (Paragraph)rtText.Blocks[0];
run = (Run)pg.Inlines[0];
txt.Text = run.Text;
pd.PrintPage += (s, pe) =>
{
double grdHeight = pe.PrintableArea.Height - (pe.PageMargins.Top + pe.PageMargins.Bottom);
double grdWidth = pe.PrintableArea.Width - (pe.PageMargins.Left + pe.PageMargins.Right);
txt.Width = grdWidth;
txt.Height = grdHeight;
pe.PageVisual = txt;
};
pd.Print(lblTitle.Text);
This simply prints the content of the textbox on the page however some of the notes are spanning larger than the page itself causing it to be cut off. How can I change my code to measure the text and add more pages OR is there a better way to do the above where it will automatically create multiple pages for me?
There are several solutions to your problem, all of them under "Multiple Page Printing Silverlight" on Google. I was having a similar problem and tried most of them. The only one that worked for me was this one:
http://www.codeproject.com/Tips/248553/Silverlight-converting-to-image-and-printing-an-UI
But honestly you should look at Google first and see whether there are better solutions to your specific problem.
Answering your question, there is a flag called HasMorePages that indicates you need a new page. Just type pe.HasMorePages and you will see.
Hope it helps
First you need to work out how many pages are needed
Dim pagesNeeded As Integer = Math.Ceiling(gridHeight / pageHeight) 'gets number of pages needed
Then once the first page has been sent to the printer, you need to move that data out of view and bring the new data into view ready to print. I do this by converting the whole dataset into an image/UI element, i can then adjust Y value accordingly to bring the next set of required data on screen.
transformGroup.Children.Add(New TranslateTransform() With {.Y = -(pageIndex * pageHeight)})
Then once the number of needed pages is reached, tell the printer to stop
'sets if there is more than 1 page to print
If pagesLeft <= 0 Then
e.HasMorePages = False
Exit Sub
Else
e.HasMorePages = True
End If
Or if this is too much work, you can simply just scale all the notes to fit onto screen. Again probably by converting to UI element.
Hope this helps

Snapshots of Control in time using VisualBrush stored in one Fixed(Flow)Document

I need to take snapshots of Control in time and store them in one FixedDocument. Problem is that VisualBrush is somehow "lazy" and do not evaluate itself by adding it to document. When I finaly create the document, all pages contains the same (last) state of Control. While VisualBrush cannot be Freezed, is there any other chance to do it? I would like to have more snapshots on one page so generate document page by page isn't solution for me. Aswel as converting VisualBrush to Bitmap (I want to keep it in vectors). In short - I need to somehow Freeze() VisualBrush
for(;;)
{
FixedPage page = new FixedPage();
...
Rectangle rec = new Rectangle();
...
rec.Fill = vb;
page.Children.Add(rec);
PageContent content = new PageContent();
((IAddChild)content).AddChild(page);
doc.Pages.Add(content);
}
I used serialization:
string svb = XamlWriter.Save(vb.CloneCurrentValue());
// Replace all "Name" attributes (I don't need them already and deserialization would crash on them) with "Tag" - not best practice but it's fast :)
svb = svb.Replace("Name", "Tag");
rect.Fill((VisualBrush)XamlReader.Parse(svb));
EDIT
Better way is to save Visual as XPS document and then take the Visual back. (De)serialization has some problems with SharedSizeGroups and many other "reference like" things.
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
control.InvalidateArrange();
UpdateLayout();
writer.Write(control);
Visual capture = doc.GetFixedDocumentSequence().DocumentPaginator.GetPage(0).Visual;

The DoubleAnimation doesn't work

//Create my grid and child controls
var layoutRoot = new System.Windows.Controls.Grid
{
Background = new SolidColorBrush(Colors.Blue),
Name = "layaoutRoot1",
Height = 400.0,
VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
};
layoutRoot.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Auto)
});
layoutRoot.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star)
});
var myImage = new Image
{
Source = new BitmapImage(new Uri(#"C:\Path\to\Image\img.png")),
Stretch = Stretch.UniformToFill,
Margin = new Thickness(3),
Width = 50.0,
Height = 50.0,
};
var textBlocklbl = new TextBlock
{
Text = "Label Here",
FontFamily = new FontFamily("Arial"),
FontSize = 14.0,
FontWeight = FontWeights.Bold,
Margin = new Thickness(3)
};
layoutRoot.Children.Add(myImage);
layoutRoot.Children.Add(textBlocklbl);
System.Windows.Controls.Grid.SetColumn(myImage, 0);
System.Windows.Controls.Grid.SetColumn(textBlocklbl, 1);
grid1.Children.Add(layoutRoot); //grid1 is placed on the MainWindow
Storyboard myStorboard = new Storyboard();
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 0.0;
myDoubleAnimation.To = 300.0;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(3));
myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
//myDoubleAnimation.AutoReverse = true;
myStorboard.Children.Add(myDoubleAnimation);
Storyboard.SetTargetName(myDoubleAnimation, layoutRoot.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(System.Windows.Controls.Grid.HeightProperty));
myStorboard.Begin();
Your animation cannot find source for animation. Setting TargetName will not work until you put animation into logical tree. Use StoryBoard.SetTarget(layoutRoot) instead of StoryBoard.SetTargetName(layoutRoot.Name).
Here is what you could do to resolve it:
1) First of all you could debug it. You just launch the code in Visual Studio in debug mode and keep looking into Output window until you see error there. I believe you will see it there soon - then you can fix it. There is still a chance that you won't see any errors.
2) Second, you should try to debug it again. Take your code, paste it into clean solution. Still doesn't work? Great! Keep removing parts of the code which doesn't look related to the issue. VerticalAlighnment, HorizontalAlignment, do you really need them? Can the issue be reproducible without those properties? Remove them. Less code - easier to debug. Ok, finally you have 10 lines of code but it still doesn't work. Take a sample from MSDN which is working and looks as close to your as possible and find the difference. You can even replace parts of your code with MSDN one to see whether it will help or not.
3) And ok, it still doesn't work, you haven't found any samples and there is no way for you to debug it - you need community help. Ask question correctly. If you are a professional developer then probably you've seen it many times - users coming with only one statement it doesn't work. You have to test/debug/fix it completely on your own. But those are your clients, they will pay you money eventually. This is free community which is driven only on behalf of our interest. If we are not interested to answer your question - we will never answer it. So ask a question which worth answering. Pasting the code which doesn't work is not enough. You should also provide an information regarding the part which doesn't work. And what have you tried to fix it yourself. If we will think that you're trying to exploit us - we won't answer your question.
Thanks for your time.
UPDATE Ok, just tried to debug it. Exactly as I said. Your code gives an exception that layoutRoot cannot be found. The exact message is: No applicable name scope exists to resolve the name 'layaoutRoot1'.. You just had to run your own code in VS, exactly as you said.

Resources