WPF MultiLine TextTrimming - c#

We have a case where we need to display Character Ellipsis(i.e. show text as trimmed) when we have multiline text.
The textblock shows trailing ellipsis when the content is anyway bigger than the width of the multiline TextBlock (i.e. TextWrapping is set to Wrap).
But we have a case wherein, we need to show only one line with ellipsis whether the text width of the first line is greater than the width of textblock or not.
For example, consider the following sample text
String str = "1\n2\n3456\n45889";
textBlock.Text = str;
The TextBlock should display as shown below:
1...
and the ToolTip will show the entire text. I tried doing some research on the possibilities but could not find much help and was wondering if anyone in the community has encountered such a situation or perhaps could suggest me?
Since, we shouldn't change the underlying data object (real time scenario) but only change what is rendered to the user, I am guessing a Converter should do the trick but I am still stuck on how to proceed. Or do you guys have any other alternatives?

Create a custom control based off of the textblock which handles the business logic needed for the ellipse.
The binding of the actual text to a specific property can ensure that the text is not changed. While in a separate property you have the visual text with the ellipse which gets updated when the original text changes (the dependency property change event) and the visual text subsequently displayed on the screen. Also have the tooltip bound to the original text which helps in that scenario of showing the actual text and not the ellipsed text.
By creating an easy custom control you have the ability to handle the business logic all in one location and it can be used in other screens and projects.

Related

Get a word location from a label and hover it with another label c#

I have a question...i'm trying to hover a specific word from a label with another label, but i don't know how to get the specific word position from the label's text. I would be very happy if you could help me at least, how to get the word location in the label. I have to say that the label Autosize is false, so it could have more than just one line. Thank you!
If I understand correctly, you want to reference one of the words in one label's .Text property and use it in another label when you hover over the first label.
If you know already what word in the string you want, it's easy. You can fetch it by the 0-indexed position of the letters, since a string is really just an array of type Char. You can also use String class tricks like .Substring() to fetch the word.
I think the degree of complexity you've added, though, is that (and correct me if I'm wrong) you want the second label to display the specific word over which the mouse is hovering while the mouse is hovering over the first label. That involves getting the label's coordinates as well as the pointer's, and a rather complex method for determining "what is a word, really?". If possible, I would split your source label into a label for each word. This can be done with dynamic labels by inheriting from the Label (or a container, like Panel) class in a custom control and giving it an IEnumerable of Labels as a property. Doing this, you can assign the OnHover handlers at runtime, and evaluate these events as individual labels rather than having to do the math and hope that a screen resolution change doesn't ruin your day.

Element adjacent to TextBox using AdornedElementPlaceholder

I have a TextBox, I am trying to place a TextBlock adjacent to TextBox using AdornedElementPlaceholder when the TextBox has focus. But after reading from MSDN, I found that AdornedElementPlaceholder can only be used for ValidationError.
I have used a Grid with column, but this is tacking the actual TextBox width that I have set in XAML, I dont want to interfere the TextBox, just want to place a element next to it, irrespective of Width.
Adding a sample image, this image is part of Error Template I created for Validation. But I cant use ValidationTemplate here.
Please put light on this question.

Including SelectionColor in TextBox

I need the property called SelectionColor in the TextBox class, for a simple Syntax Highlighter - I can't use directly a RichTextBox - it causes too many problems, that's why I try to do this.
Is there any way to make that property available for a TextBox?
If it isn't possible, I'd try to write my own, but I need an idea on how to do it, basically how it works - is it based on drawing strings over the original text?
Thanks in advance.
Basically, using a TextBox for anything but plain text is a bad idea. First of all, you will eventually get a new feature to implement which is not present in TextBox and you will have to handle it manually. After some time you will implement a custom RichTextBox or something similar.
Second, it is relatively hard to even solve the problem you mentioned. Technically, you can override painting function (which you have to do if you want new functionality for TextBox). You can then let TextBox paint itself and paint the colored text above the image. But don't do it. You will get two (maybe more) problems:
Flicker of image. Once the original textbox has drawn itself, the image can be shown on screen (if you don't use double buffering).
Text alignment. It is hard to place colored text exactly above black text, plus you can run into problems with text rendering: you will need to clear area you're drawing in.

Scrolling all but the top line in a multiline textbox

I have a multiline textbox in a WinForms application. What I'd like to do is always have the top line visible, even if it scrolls. Is there some trick someone knows to do this?
Fake it. Use two TextBox objects, draw your own borders. You will need to deal with wrapping to the next line yourself.
You could also copy the first X characters to a label so when the TextBox scrolls they can see the first line in the label.
Unless it is an essential feature I would try to cut it.
The simple answer; depending on the appearance you are going for is to use existing windows controls to get the effect you want.
You can use a label control above a textbox and allow the textbox to scroll.
You can use two textbox - the top with it's .multiline property set to false, while the bottom allows scrolling.
You could encapsulate this all into a user control for reuseability.
Beyond that, I think you'd be looking at a fairly large project to implement your control (or at least overriding the onPaint() event of the textbox) with the desired behavior.

WPF Textbox "normal" text input

G'day,
I'm not sure if this is a problem relevant to only me or if anyone else has this issue also. None the less, I'll try and describe what is going on here.
I have a few textbox's, default style, etc. I set an explicit maxwidth and maxheight to prevent resize when the text exceeds the default width of the textbox. The issue is that the text wraps to the next line, but I only want single line. So I set maxlines to 1 and textwrapping to NoWrap. That's fine.
Now the carat and typed text disappears under the edges of the textbox when the width is exceeded and the only way I can get the carat and newly typed text back into view is by pressing the left and right arrows. Coming from MFC and using textboxes all the time with HTML, I would have thought the default behaviour would be to have the textbox content scroll with the carat or am I missing something here?
Thank you,
Ash
What you are requesting is actually the default behavior. Start with an empty grid and place a textbox on it. Type some text into the box and the text view will scroll with the caret.
Below is the XAML I tested with, perhaps you have a style interfering with it?
<Grid>
<TextBox MaxWidth="20" Height="20"></TextBox>
</Grid>

Categories