WPF: Drawing a single character in a Rectangle - wpf

this time, I have a rather short question: I want to Have a single char (which is a bracket) to display in a given Rectangular area without having spaces between the char an the border of the Rectangle. Is this possible?
//EDIT: What im trying to do is placing brackets around a mathematical term which might be of different height.
greetings

Something like this should work:
<Border Background="AntiqueWhite">
<TextBlock Text="?" />
</Border>
I've set the background of the border so you can see it. Normally you'd want to leave it along or have it transparent.

Related

Hide UIElement but display that element's Effect

I am drawing a sequence of lines with DropShadowEffect in WPF. I want all the effects to display behind all the lines. So I drew the sequence twice: The first time I set Line.Stroke to Transparent and added the effect, and the second time I set Line.Stroke how I wanted and left off the effect. Unfortunately, a transparent stroke seems somehow to hide the effect as well, so my first sequence of lines doesn't display anything. Setting StrokeThickness to 0 also hides the effect. How can I show Line.Effect without showing the Line itself?
As commented, WPF - Element is invisible but has dropshadow effect asks the same question I do, but I want a code solution if possible, not just a Blend feature.
A dropshadoweffect is a sort of gaussian blur.
It takes pixels and sort of copies them offset.
You need something to copy.
So the answer is no, you cannot.
You could maybe try clipping the line to remove it, but I would think that's going to give you a hole where the line was.
In fact if you take the question as stated then the shadow is a blurred version of the line offset by a few pixels.
If your line is say:
<Line X1="0" X2="100" Y1="0" Y2="150"
StrokeThickness="5"
Stroke="Black">
<Line.Effect>
<DropShadowEffect/>
</Line.Effect>
</Line>
The shadow is something like:
<Line X1="4" X2="104" Y1="3" Y2="153"
StrokeThickness="5"
Stroke="Black">
<Line.Effect>
<BlurEffect/>
</Line.Effect>
</Line>
If this is not exactly what you want I could give you code which does a box blur on a black white image.
It's not as neat as gaussian but gaussian is hard to code. You'd have to paint your lines then blur em.
For interest
This is an explanation of the gaussian algorithm:
https://blog.en.uwa4d.com/2022/08/11/screen-post-processing-effects-chapter-1-basic-algorithm-of-gaussian-blur-and-its-implementation/#:~:text=Gaussian%20Blur%2C%20also%20known%20as,pixel%20values%20in%20the%20field.

Trying to wrap and scale text in a textblock WPF

I'm trying to auto scale the font and wrap text in a TextBlock in WPF and I can't figure it out.
I've Googled it and looked at stackoverflow loads of times and the main suggestion is to place a TextBlock inside a ViewBox. I've tried that, and all it does is scale the whole text down to one line instead of wrapping it.
If I just use a TextBlock without a ViewBox it wraps, but doesn't scale to fit. It's driving me mad, as I am literally trying to move from WinForms to WPF to make better looking UIs.
I've tried StackPanel and DockPanel and they still don't have the desired effect.
All I want is a TextBlock to take a string of text of unknown size and display it scaled and wrapped. I don't understand why it's so difficult
It is helpful to include code of what you have tried.
When I do this:
<Grid>
<TextBox VerticalAlignment="Center" TextWrapping="Wrap" Width="100"/>
</Grid>
I get this:
Is that what you are looking for?
You could also check out the RichTextBox if you need more features.

WPF control origin point

After some investigation, I still can't find method to change origin of control.
So, I want just to place one square exactly in center of another square, without margins, so it will be completely independent of first square size.
Theoretically, it can be easily done with HorizontalAlignment and VerticalAlignment set to Center, since it automatically sets Margin of control to half of width and height of parent control. But it is not so simple.
Simplest way to describe problem is next picture
As you can see, margin is counted towards upper left corner. Which is what I call origin. The perfect solution is to change it to center of first square, but this is where I need help - how can I do that?
Point of origin applies when using a transform object, and attaching the transform to your control. It won't actually effect the behaviour of the margin or left, top properties. If using a transform to place your object, point of origin is very useful.
The top, left (if using cavas) and margin (if using say grid) help govern the "auto" placement by the parent control, and this in turn governs where point of origin for the control winds up being relative to the parent control. The transform object then offsets RELATIVE to where that point of origin is.
The other useful thing is that transform overrides the auto placement in the parent control, or rather, forces an offset to where the parent wants to put it, which in some cases is useful - i.e., you might have boxes listed in a grid and want them to "shake" left and right when you hover the mouse over them, their alignment stays in order to the grid, but the transform lets you bump them away from their "forced" position.
For example, attach the same transform object to 2 controls, and set their origins separate, then apply an animation to the transform object - both controls will animate off the one animation object (if you wanted to their movement in perfect sync).
Well, it was weird enough. The given behaviour can be seen only when using Image, and Center alignment. Can be solved by either wrapping Image in Grid, which will be using Center alignment, or using Stretch alignment with Image (which is much simplier).
<Grid Width="500" Height="500">
<Image Width="250" Height="250" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
If you want to reproduce problem I've described in question, replace Stretch with Center in code above.
Probably oversimplifying here but I would just use a Grid to wrap the two items you mentioned like this example (One stretched to fit and one centered):
<Grid>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Black" BorderThickness="10" Margin="4"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Padding="10">InnerButton</Button>
</Grid>

WPF: how to write text in a different direction?

I need to write text in the orientation specified for the image below. The fact is that I saw some examples around here by using a textblock and rotating the angle of the control using "RenderTransform", but this is not what I really need. I tried to do it using an image but it doesn't fit very well... so I really don't know how to solve it. If you look at the image beside you can see that the text is written from bottom to top and the line below the text is in the right of the screen.
This is the screen that I need to develop:
I tried by rotating the textblock, but the only way that it works for me was wrapping the text, but this is just the "closest" solution that I found. Also, as you can see, I need to set a border for the textblock.
Anyway, I hope you can help me because any example around fits with my problem.
In order to rotate your text at 90 degrees, I believe that you will need to use the LayoutTransform instead of the RenderTransform:
<TextBlock Text="FootRoller" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90"/>
</TextBlock.LayoutTransform>
</TextBlock>
The difference is when the transform will be applied. Using the LayoutTransform, the text will be rotated before the layout pass and this will be important in your case. I imagine that using the RenderTransform will rotate your TextBlock, but as it does that after the layout pass, it would not show it all... this is because it was measured for size before it was rotated.
You can find out full details from the Transforms Overview page on MSDN. From the linked page:
LayoutTransform – A transform that is applied before the layout pass. After the transform is applied, the layout system processes the transformed size and position of the element.
RenderTransform – A transform that modifies the appearance of the element but is applied after the layout pass is complete. By using the RenderTransform property instead of the LayoutTransform property, you can obtain performance benefits.
They're all right. RenderTransform should be all you need. Like;
<TextBlock Text="FootRoller" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.RenderTransform>
<CompositeTransform Rotation="-90"/>
</TextBlock.RenderTransform>
</TextBlock>
P.S. - You can literally just change RenderTransform to LayoutTransform which Sheridan has provided an explanation for in his answer.
If RenderTransform didn't work, take a look at LayoutTransform. You didn't tell us why RenderTransform didn't work but it's usually a safe bet that LayoutTransform will solve whatever problem it gave you.

Removing a border line in Silverlight Border control (kind of like border-style-bottom:none in css)?

I'd like the ability to have something that looks like a border but I don't
want the bottom to show. in CSS I could accomplish this by going border-style-bottom:none
I'm wondering if Silverlight has a similar functionality for the Border control.
If not, I'm wondering what other control I can use to accomplish this? thanks
It works like this:-
<Border BorderBrush="Black" BorderThickness="2,2,2,0">
...
</Border>
Another form just takes two numbers "lr,tb" which specifies the thickness of the left and right of the border then the top and bottom of the border.

Resources