Why is the content of this label not centered? [duplicate] - wpf

This question already has an answer here:
Getting text to sit flush against the top of a TextBlock (Not VerticalAlignment, I promise!)
(1 answer)
Closed 2 years ago.
Consider the following xaml:
<Window x:Class="DemoApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="DemoApp.MainWindow" Height="450" Width="800">
<StackPanel Orientation="Horizontal">
<Label BorderThickness="1" BorderBrush="Black" Width="100" Height="100" FontSize="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0">A</Label>
</StackPanel>
</Window>
As shown in the image below, there is plenty of space within the label to accomodate the letter, yet it appears that the letter does not center as per the VerticalContentAlignment. What is the cause of this?

I think VerticalContentAlignment has no problem.
Problem is that your Label is smaller than Font size.
<Label BorderThickness="1" BorderBrush="Black" Width="100" FontSize="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0">A</Label>
Try this code.
I removed code "Height=100" and height is auto.

Related

Size of Grid is NaN, wpf [duplicate]

This question already has an answer here:
Why is my Grid's width NaN?
(1 answer)
Closed last year.
I want to get size of Grid, but I get NaN..
Here is the xaml file:
<Window x:Class="TestWPFHCI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPFHCI"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Title="MainWindow" Height="700" Width="1024" MinHeight="700" MinWidth="1024">
<Grid Background="#FFEEEEEE">
<ScrollViewer x:Name="scrollOnGrid" Margin="280 55 10 10" HorizontalAlignment="Stretch" VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto">
<!--<Grid Name="MCGrid" Margin="100 100 50 50">
</Grid>-->
<Grid Width="Auto" Margin="10 10 10 10" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="MCGrid">
<!-- <Grid x:Name="MCGrid"/> HorizontalAlignment="Center" VerticalAlignment="Center"/> -->
</Grid>
</ScrollViewer>
</Grid>
</Window>
And here is the code for get grid size, but I always get NaN.. Any idea for solution?
int width = MCGrid.Width;
Use MCGrid.ActualWidth to get the width of Grid.
double width = MCGrid.ActualWidth;

ScrollViewer is not working for TextBlock in WPF

Friends, below is my code. I don't know why my scroll bar is not working.
<Window x:Class="Seris.Views.Help"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Help" Height="400" Width="400">
<DockPanel>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="350" Width="372"><InlineUIContainer>
<Image Height="100" Width="100" RenderTransformOrigin="1.37,0.46" Source="../Images/help.jpg"/>
</InlineUIContainer><Run/><LineBreak/><Run Text="There are some validations on the Vehicle Form as below."/><LineBreak/><Run Text="P"/><Run Text="lease note that all the fields are mandatory."/><LineBreak/><Run/><LineBreak/><Run FontWeight="Bold" Text="Vehicle No"/><LineBreak/><Run Text="It should be always 7 character long con"/><Run Text="taining "/><Run Text="first 3 digits as alphabet in capital form and remaining 4 numerical. "/><LineBreak/><Run Text="i.e. GHI1234"/><LineBreak/><Run FontWeight="Bold" Text="Model"/><LineBreak/><Run Text="It can be anything except null."/><LineBreak/><Run Text="i.e. Fluid"/><LineBreak/><Run FontWeight="Bold" Text="Manufacturing Date"/><LineBreak/><Run Text="It should not be a future date."/><LineBreak/><Run FontWeight="Bold" Text="IU No"/><LineBreak/><Run Text="It must be 10 digit numerical number."/><LineBreak/><Run FontWeight="Bold" Text="Personnel Name"/><LineBreak/><Run Text="You must select Personnel Name from combo box."/><LineBreak/>
</TextBlock>
</ScrollViewer>
</DockPanel>
</Window>
Remove Height="350" & Width="372" from the TextBlock, these are preventing ScrollViewer to work properly
if you want to restrict the size you may apply the same to DockPanel or ScrollViewer as needed.
so in short if you apply width or height to TextBlock that will restrict the size of the element and ScrollViewer may not work as expected.
additionally you may also remove HorizontalAlignment="Left" & VerticalAlignment="Top" from the TextBlock as they might not be required as well

Window set to size around content leaves gap on each side

Can someone please tell me why, even though I've set my Window to SizeToContent and my content is Width=40 does the app load bigger across the Width?
The following example sizes to height, but the width is about 100 pixels (estimation).
Thanks
<Window x:Class="WpfApplication2.RetailButs"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RetailButs" SizeToContent="WidthAndHeight"
WindowStyle="None" MinWidth="0" >
<Grid x:Name="LayoutRooty" Width="40" Height="40">
<Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >
</Border>
</Grid>
</Window>
The extra size you have got comes from 3 top right window buttons.
So the best solution is just take buttons out (1) and put some minimum size for the window (2).
Try this and it will be exactly what you need.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="40" Height="40" SizeToContent="WidthAndHeight" WindowStyle="None" ResizeMode="NoResize">
<Grid x:Name="LayoutRooty" Width="40" Height="40">
<Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >
</Border>
</Grid>
</Window>
There was a similar question recently which i was unable to find. The size is that which would be needed if the WindowStyle were the default 3D border with all the buttons which take up a lot of space. I think there is some way to update the layout, maybe you can figure something out.
Edit: Set the window's Width="0" (or any other value for that matter) and it should update correctly. The size to content will override the value.
(If you set the WindowStyle to ToolWindow it always fits correctly)

wpf textblock text is not clear when using layouttransform

I am using TextBlock control. Text in the TextBlock is clearly displayed with 0 degree rotation.
But if i rotated the control to 90 degree using LayoutTransform, text is not clear. some blurry display.
Is there anyother way to rotate the text without LayoutTransform or anyother way for clear display?
try using "UseLayoutRounding=true" on your TextBox
I know this is an old question, however, I had the same problem with a user control which had SnapsToDevicePixels="True" and UseLayoutRounding="True"
with the code below the first label showed a blurred box, while the non-transformed one showed the text perfectly. I tried flowing up the snaps and rounding properties up to the hierarchy and in the end, the only thing that fixed this behavior was to apply UseLayoutRounding=" True" to the window. Applying it to any other children panel or user-control did not fix it.
<UserControl x:Class="MyApp.Controls.Indications"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
SnapsToDevicePixels="True"
UseLayoutRounding="True">
<Stackpanel>
<Label
BorderBrush="Black"
BorderThickness="1"
HorizontalAlignment="Center">
<TextBlock
Width="100"
TextAlignment="Center"
Text="Left Outboard Actuator"
TextWrapping="Wrap">
<TextBlock.LayoutTransform>
<RotateTransform
Angle="90"/>
</TextBlock.LayoutTransform>
</TextBlock>
</Label>
<Label
BorderBrush="Black"
BorderThickness="1"
HorizontalAlignment="Center">
<TextBlock
Width="100"
TextAlignment="Center"
Text="Left Outboard Actuator"
TextWrapping="Wrap">
</TextBlock>
</Label>
</Stackpanel>
</UserControl>
Set TextBlock's Foreground property a value(like Black) will be clear. it's worked on my project when I rotate textblock 90 degree.

WPF TabIndex in a composite control

I have a simple window with a simple composite control embedded within it.
(Main Window)
<Window x:Class="TabOrder.Window1"
xmlns:local="clr-namespace:TabOrder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label>
<TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label>
<TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
<local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/>
</Grid>
(Composite control)
<UserControl x:Class="TabOrder.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label>
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label>
<TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
</Grid>
As expected on my form I get 4 text boxes...
First
Second
Third
Fourth
But when "First" has focus and I hit tab the focus is switched to "Third". WPF seems to be seeing the tab list as a single flat list rather than as a tree where MyControl is TabIndex 3 and the text box "Third" the first tabbed control within it.
Is this a bug in WPF or is there another way of doing this? The composite control is used in many windows, it could even be used more than once on a single window.
I know this response is quite late... but have you tried:
<UserControl ... KeyboardNavigation.TabNavigation="Local">
Doing so will ensure once your UserControl has recieved focus, you will navigate only through TabStop within your UserControl (instead of worring about conflicting TabIndex values throughout your app). After looping through the TabStops of your UserControl, TabNavigation will resume to the TabStop outside of it.
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx

Resources