WPF: Space in TextBox between border and text - wpf

I have a TextBox with Width equals the width of a letter 'A'. But the letter is not displayed correctly.
Is it possible to avoid space between a border and the beginning of 'A'? Padding="0" does not help.
Example:

When you inspect visual tree with a tool like Snoop you can see that TextBox consists of Border that contains ScrollViewer that among others have ScrollContentPresenter that renders actual content of the TextBox with TextBoxView (1) internal class handling the rendering of content in the textbox and by default left and right margin set to "2" (2).
I don't think you can style the TextBoxView itself to override default margin values but as it is internally set to "2" you can safely assume that setting Padding on Textbox to "-2,0" will be fine.
Edit:
In TextBoxView documentation
you can find that in TextBoxView constructor (line 38) there is Margin property set to the constant value of BidiCaretIndicatorWidth (line 1219):
So probably it make sense to leave some space at the end and the beginning of the TextBox so the caret can be visible
static TextBoxView()
{
// Set a margin so that the bidi caret has room to render at the B edges of content.
MarginProperty.OverrideMetadata(typeof(TextBoxView), new FrameworkPropertyMetadata(new Thickness(CaretElement.BidiCaretIndicatorWidth, 0, CaretElement.BidiCaretIndicatorWidth, 0)));
}
(...)
// BiDi caret indicator width.
internal const double BidiCaretIndicatorWidth = 2.0;

Related

How do I auto resize a label's text in WPF?

I put my label inside a two-column grid and set its size to auto and when I run my application, the label's size grows if I maximize it (I'm sure it grows because I've put a background color and the background color also resizes to accommodate the entire two-column grid it's been placed into. My problem is that the label's text itself doesn't grow and stays the same 9pt. Am I missing a property option to set my Label's text to also auto-resize?
I've read tutorials doing this with a viewbox and a textblock and although it works, I've had trouble setting it up in two columns as it resizes weirdly when I run the application despite setting it up right.
Okay I finally did it. What I did was use the Viewbox + Textblock and I've put a border around the viewbox and set its background accordingly and had the textblock's background to none and now it's working as I wanted it to.
The problem I had back then was I didn't know how to put a background color on the viewbox so I tried a remedy wherein I changed the textblock's background but the problem was the textblock has its own size and even if I stretch it, it would only encompass the width of the text and not the whole viewbox.

Take object "out of the document flow" in Silverlight, with the same effect as CSS Absolute Positioning?

When using Silverlight 4, is it possible to set an Image to an absolute position, and bring it out of the document flow, allowing it to be positioned freely of any grids etc?
With CSS you can set an element to use absolute positioning, and then it will be positioned absolute, based on the first relative parent above it.
I want to be able to place an Image, anywhere on the screen, above anything else on the page, but in Silverlight.
I tried absolute positioning (In the code behind) and it doesn't seem to be positioned correctly, it looks as though it defaults to both Horizontal Alignment and Vertical alignment as
"Centre"
CustomIcon.Source = new BitmapImage(new Uri("http://media.trueachievements.com/imagestore/0000149800/149834.jpg", UriKind.Absolute));
CustomIcon.SetValue(Canvas.LeftProperty, Pt.X);
CustomIcon.SetValue(Canvas.TopProperty, Pt.Y);
CustomIcon.Visibility = System.Windows.Visibility.Visible;
Pt is set correctly elsewhere (Checked this when Debugging).
The image is in the Xaml with the x:Name attribute set, and is set to Collapsed visibility by default.
Any ideas if it's possible to get the same effect I described (CSS) but using Silverlight 4?
The canvas Left and Top attached properties only have an effect if you actually add the control to an Canvas element.
Just add a Canvas element to your xaml as the last element in the "LayoutRoot" grid. You do not need to set its width or height nor should you set it Grid.Row or Column.
Now when you add items to this canvas they can be positioned anywhere.

RichTextBox color formatting without moving visible view

Let's say that I have a RichTextBox and its contents take up about 3 times the height compared to the visible height. There's no color formatting, I want to highlight keywords. If I use SelectionStart, SelectionLength and SelectionColor, then I have to set SelectionStart back to the caret's original position.
If, for example, I'm looking at the first page and my caret is half-way down the page, but I want a keyword highlighted near the end, when the caret is returned, the RichTextBox will only scroll up far enough for the caret to be on the top visible line, so my visible position has moved.
Is there any way for me to format colors out of view without affecting the view position? Or, is there any way for me to get and set/reset the view position after formatting?
The solution is here:
"Get/Set the ScrollBars’ positions"
http://codebetter.com/blogs/patricksmacchia/archive/2008/07/07/some-richtextbox-tricks.aspx
After I return the caret to its original position with SelectionStart, I can use the code shown on the blog to return the view to its original position.

How to determine width of a windows forms controls whose width was increased because it is Anchored?

I currently have a Textbox on a Windows Forms, and I need to dynamically add a PictureBox box control at the right of the Textbox.
I create the PictureBox programmatically and I when setting the location of the PictureBox, i'm setting like this:
pBox.Location = new Point(tbControl.Location.X + ctrl.Width, ctrl.Location.Y);
So i'm setting the picture box to be located at the X location of the textbox PLUS the width of the textbox. However, since the textbox has an anchor property set to right, its width increases to fill the space between itself and the form border.
Problem is, that even though the textbox's width is visually bigger than the actual value of Textbox.Width. the Width property is not taking into account the extra width of being anchored.
I already tried properties like Textbox.Bounds.Width, Textbox.ClientSize.Width, Textbox.DisplayRectangle.Width, etc. with no luck. All of those properties return the original Width of the control without taking into account the resized width due to the Anchor property.
Does anyone know how I can determine the real size of the textbox? Thank you
The Width property always tracks the current width of a control, whether it is anchored or not. However, the TextBox is going to grow when you make the container larger and that will make it overlap the PictureBox. You have to anchor the PB to the right as well.
These should be returning the adjusted size. Either you are referring to the wrong textbox, or you are doing the query before the size has actually changed.

WPF - setting HorizontalAlignment= Stretch to Textbox in StackPanel

Why doesn't a textbox stretch to fill space in a stackpanel? Is this by design? In a grid, the textbox stretches as expected.
Yes, it's by design. The StackPanel will allocate the space the TextBox asks for. If you haven't set a width on the TextBox, it will require only enough width to fit its text.
Kent's answer seems right.
To still force override the StackPanel behavior, I think you'd need to dynamically compute-set the Width property of the contained elements OR some funky override of MeasureOverride. I'd rather use another layout manager/panel. Some things I noted..
The default value for HorizontalAlignment and VerticalAlignment properties of child elements is Stretch (if you don't specify one explicitly).
The StackPanel will stretch elements based on its Orientation property value. So
Orientation=Horizontal means all elements will be vertically stretched to max. Elements flow horizontally.
Orientation=Vertical means all elements will be horiz stretched to max. Elements flow vertically.
Unless explicitly specified, Width and Height of child elements are NaN. If you specify an explicit value, StackPanel will honor them over the Horiz and Vert Alignment settings.
The StackPanel itself has HorizontalAlignment and VerticalAlignment that adds a further layout twist. You can experiment with this example.
StackPanel
The default value is stretch for both
HorizontalAlignment and
VerticalAlignment of content that is
contained in a StackPanel.
HorizontalAlignment
When Height and Width properties are
explicitly set on an element, these
measurements take higher precedent
during layout and will cancel the
typical effects of setting
HorizontalAlignment to Stretch.
I needed items to be sized evenly, but stacked vertically.
I used a UniformGrid, and set the Columns property to 1. (tested with a TextBox, and it stretches like you want)

Resources