create custom control which include a text and a image - wpf

I want to show a lot of (hundreds of or more) controls in a window, the control include a text and a image and have 4 different appearances (different text color and image), when user click the control, its appearance will switch among the 4.
In order to improve performance, I want to create a custom control and try to choose a good lightweight base class for it, after googling, seems UIElement is the most lightweight, which involving call DrawText and DrawImage on DrawingContext object, however, here indicate the DrawText is expensive.
Another choise is control such as ContentControl, TextBlock or Button, but they are more heavy than UIElement, does anybody have any advice? many thanks.

Related

Create a screen like control panel windows forms 2010

I am building an application and I would like the one of the forms to look like the control panel
Would you use a listview with groups?
thanks
No, I don't think you can make the ListView do this. At least not without rewriting its painting routines.
It isn't going to be easy to replicate this layout precisely in WinForms. There's nothing you can just drag over from the toolbox that is going to do it. It is possible, but it will take some effort.
What I would do, conceptually, is break it up into pieces. I count 8 different repeated "blocks". So I'd create a UserControl that models each of these.
Inside of the UserControl, I'd have a PictureBox control to display the icon, a Label control to display the heading, and a TableLayoutPanel that can be filled with the desired number of LinkLabel controls. Set all of the properties of the controls so that they match, and then write public methods to set the icon, heading, and add/remove links.
Then I'd drop a TableLayoutPanel onto my form, set the margins to have lots of whitespace, and add as many of those UserControls as necessary. At the top of the form, of course, you'd need another Label to display the heading/instruction text. Make the form's background white, and you're pretty much there.
The logic to switch between views isn't implemented, but consider whether you really need that. It is important in Windows because users are used to the old way of doing things. This doesn't apply to you.

Highlighting text in a textbox without using RichTextBox control

I have a WPF textbox (not a RichTextBox) and inside this textbox, I want to highlight search results (like in web browsers)
For example if I search for "abc", all occurences of "abc" should be highlighted (for example, with some red background or font)
I want to know if this is possible without using RichTextBox control, or not really?
It's possible but it's much more easy to use a RichTextBox so you may consider to use that instead, moreover you cannot change font size but only color (background and/or foreground) and effects.
First you have to derive your own class from TextBox because you'll override its render method. Now override the OnRender() method, here you'll use the DrawingContext.DrawText() method to draw the text (place everything inside a FormattedText object, primary you'll have to adjust its properties to make it similar to a standard TextBox).
Now what you have is a plain TextBox where in addition you draw your text. From this starting point you can choose to:
Completely override TextBox text drawing: set TextBox.Foreground property to Brushes.Transparent. User will interact with "real" text but he'll see the text your draw. Please note that to make this works you have to mimic exactly how text is drawn (if you change font size, for example, then they'll be unaligned) in the original TextBox.
Add the highlight feature you need keeping the base TextBox text drawing: calculate where the text you want to highlight is and then draw the proper background.
References
This (simplified!) algorithm comes from CodeBox2, it was originally designed to extend a TextBox with some simple editor-like features.
There is no built-in functionality for this. Also, the TextBox only supports a single fontstyle for the entire text.
If the text should be read-only, you could use a flow or fixed document and format the text in Run Elements.

WPF layout Control issue

I have wpf project which print label as per editor. Project has 17 different editors. This is small little drawing for my current application for current label editor.
It has several small controls like, Tool Box, Dimension Slider, Ruler, Canvas navigation bar, Label Definition bar etc... It has one more control called Label Editor, this control will vary as per different editors (17), other controls will stay as it is.
No I want to make some generic control which contains all controls except Label Editor, so each time when I create new editor I don't need to re-create all controls each time. I can use what I have for all 17.
What control should I take to do that, content control, control template, data template or user control for that. Here is small drawing what I have.
Pls share your suggestions.
Thanks
Dee
You could try loading the label editor through a Frame and create a page for each label editor.
Alternatively, you could create a custom control with a LabelEditor property that takes an object. This approach would be more difficult, though.

Multiline WPF textblock: different alignment for lines

Is that possible to set different horizontal alignment for different lines of multiline textblock?
For example, I want to center header of my text, but main text I want to align by left side. I'd not want to use several textblocks for this issue.
Thanks.
I do not think you can get what you are looking for using the TextBlock control, this because the Inline elements that you can add in your TextBlock does not allow you to control the horizontal alignment (line by line).
Anyway I do not think that this is the best approach, in fact, as specified by MSDN:
TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.
So if you want to have more flexible control, and also better in terms of performance for what you ask it to do, you should use the FlowDocument.
I want to add a final remark. If you're looking for a dramatic improvement in performance, and you think that a single TextBlock is the right solution, I suggest to use Visual elements and the relative DrawText method instead. But if you're looking for an easy control to maintain in the future the choice is FlowDocument, as already said.

WPF page layout control selection

I have an imagebrush of a soccer field as the page background and I want to be able create a line up by dragging players off the bench and positioning them on the field accordingly. I don't know what control to use for the background that allows the PlayerCard control to reside where it is dragged. Any help as to how to begin would be appreciated.
You can get the idea here.
What control to use?
I believe you want the Canvas container control. It lets you arbitrarily place child controls with a Left and Top attached property, similar to the way Windows Forms does it.

Resources