I'm not able to center text vertically on a default button (no styles or templates used)
I saw threads like
Text content in a WPF button not being centred vertically
^^ I'm not setting any text height atleast in button 1.
Not sure what causes this behavior and how to get rid of it efficiently when localisation comes in.
I do get I can set a -ve margin to align text exactly, however that might screw things up for another language. Is this some limitation based on the Default Font Family Expression blend seems to impose?
Any help with this would be great. Seems trivial but cant seem to find a decent explanation for what's causing this behavior.
Xaml code is as follows:
<Window x:Class="TestButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="ButtonFFAH" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" VerticalContentAlignment="Center"/>
<Button Content="ButtonAH" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"
Margin="80,0,0,0" Height="15.627" VerticalContentAlignment="Center"/>
</Grid>
It`s how it works internally. If you want some more control, just fill button content with more customizable things that just a string. Like this:
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"
Margin="80,0,0,0" Height="15.627" VerticalContentAlignment="Center">
<TextBlock VerticalAlignment="Center">ButtonAH</TextBlock>
</Button>
The apparent vertical misalignment is due to the fact that all kinds of accents or other diacritical marks add to the total font height. Although the actual Button content may not contain such characters, the vertical alignment has to take this into account to ensure a common baseline alignment with other Buttons with the same "outer" alignment, for example in the same Grid row.
Related
For example Button, Link, Image ,...
This code does not work:
<Grid>
<TextBox
HorizontalAlignment="Left"
Height="180"
Margin="38,35,0,0"
TextWrapping="Wrap"
Text="TextBox"
VerticalAlignment="Top"
Width="442"
>
<Button
Content="Button"
HorizontalAlignment="Left"
Margin="149,105,0,0"
VerticalAlignment="Top"
Width="75"
RenderTransformOrigin="-2.78,1.376"
/>
</TextBox>
</Grid>
Not without writing your own custom control. That is the proper solution.
Here is a good example of a custom control for this: https://stackoverflow.com/a/6419556/1624581
Alternatively, as a hacky and quick solution you could put the button and the textbox in a container (e.g. stackpanel or grid) and then use a negative margin on the button so that it appears within the textbox. You can then apply a padding on the right of the textbox so that the input text doesn't overlap with the button. With that said, I'd strongly recommend writing a custom control as negative margins can cause maintenance headaches in the long run.
In the following XAML code the button text is half missing. I can change the Margin property and it becomes obvious that after 250px the content is hidden. Why is that, and how can I fix it?
<Window x:Class="InnerInterface.InventoryManagement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="someWindow" Height="500" Width="500">
<DockPanel HorizontalAlignment="Left" Name="dockPanel1" VerticalAlignment="Top">
<Grid DockPanel.Dock="Top">
<Button Name="buttonReturnToMainMenu" Content="someButton" Margin="200,0" Width="125" />
</Grid>
</DockPanel>
</Window>
You have a horizontal margin of 200, and a button width of 125, which means the total width needed to show the control properly is about 525.
You also have HorizontalAlignment=Left" on your DockPanel, which means it will draw the content at whatever width it needs and align it to the left side of the screen instead of stretching it to fill all available space. This means it is blocking out a space of 200 on either side of the control, and drawing the button in the remaining space. If this remaining space is less than 125, the image will be cropped.
If you switch to HorizontalAlignment="Stretch", then it will draw the control first (with margins), then stretch it's size so it fits all available space, so the entire control gets resized rather than cropped.
You might be interested in reading this MSDN article on Alignment, Margins, and Padding in WPF.
Edit
If you want only the Left margin to be 200, then use Margin="200,0,0,0". Using Margin="200,0" means that both the left and the right Margins will be 200.
Not really sure about your exact problem, but maybe this should help:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<DockPanel HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Top">
<Grid DockPanel.Dock="Top" >
<Button Name="buttonReturnToMainMenu" Content="someButton" Width="125" />
</Grid>
</DockPanel>
</Window>
The problem is that the button Margin is set as:
Margin="200,0"
It should be set as:
Margin="200,0,0,0"
This eliminates the margin on the right side and allows the whole button to show.
I have a page in my WP7 app consisting of a TextBox beside a ListPicker. In their default modes, they don't line up properly; the ListPicker has a different padding to the TextBox, and its height is also different.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Horizontal">
<TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<toolkit:ListPicker Width="170" ItemsSource="{Binding l}" Style="{StaticResource ListPickerStyle1}" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
Manually tweaking the ListPicker's template to fit in correctly here is tricky and error prone. For example, when its height is adjusted, the caption (i.e. the text of the selected item) is no longer in the centre of the component.
My app is currently failing MS app review because the components are not all the same height.
Is there an easy way for me to set the toolkit:ListPicker to have the same appearance as a TextBox?
The simplest solution will be to take a copy of the the default style and tweak that using Blend to be exactly how you want it to look. This will take a little trial and error to sort out.
You can then use the implicit styling rules to apply it to all ListPickers without having to explicitly set the style on each instance:
<Style x:Key="MyListPickerStyle
TargetType="toolkit:ListPicker>
.... your tweaks here
</Style>
<Style TargetType="toolkit:ListPicker"
BasedOn="{StaticResource MyListPickerStyle}" />
It may be easier to tweak the TextBox Style of course :)
I have looked at several code snippets where people suggest that the AcceptsReturn property of a textbox in Silverlight will enable multiple lines.
My problem however is when I add a textbox with said property and explicity set the height or allow it to fill the container, the text sits vertically in the middle of the textbox.
<Grid x:Name="LayoutRoot" >
<TextBox TextWrapping="Wrap" Text="TextBox" AcceptsReturn="True"/>
</Grid>
I need the text to anchor to the top of the textbox.
Ensure there is not an implicit style for the text box which is overriding the default expected behavior in this case. IN my case I was using the Cosmopolitan Theme from Microsoft and it had an implicit style for TextBox elements that did not produce the proper behavior.
In the resources from that theme if you look at the DefaultTextBoxStyle in the CoreStyles.xaml file, at line 448 you will find the ScrollViewer with a VerticalAlignment set to Center. Adjusting this to top solved my problem.
Try this:
<Grid x:Name="LayoutRoot">
<TextBox VerticalAlignment="Stretch" VerticalContentAlignment="Top"
TextWrapping="Wrap" Text="TextBox" AcceptsReturn="True"/>
</Grid>
The text is at the top of the box, and the the box stretches to fill the whole page.
This is supposed to be a no brainer but I still can’t figure it out.
In my sample app there’s a button and a textbox in a dockpanel. If the content of the textbox is smaller than the content of the textbox the window is as big as it needs to be to display the content of the button. That’s what I want. But if I put more text into the textbox the window gets wider :-(
The behavior I want is that the window gets the width according to the buttons content and the textbox wraps its content (or/and shows scrollbars if necessary).
Thank you!
Some sample code:
<Window x:Class="SO1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" SizeToContent="Width" FontSize="20">
<DockPanel>
<Button DockPanel.Dock="Top">A rather long text</Button>
<TextBlock TextWrapping="Wrap">Short text</TextBlock>
</DockPanel>
</Window>
Having tried it, it seems that binding the TextBlock's MaxWidth to the ActualWidth of the Button achieves the effect you're after:
<Button x:Name="btn" DockPanel.Dock="Top">Short text</Button>
<TextBlock TextWrapping="Wrap"
MaxWidth="{Binding ElementName=btn,Path=ActualWidth}">A rather long text</TextBlock>