I’ve got a button (NOT created by xaml) for which I’m setting the content alignment like this:
btn.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
but if the content is too long then the text is sliding to the right after a point in which you can’t see the text anymore.
the buttons are created inside panorama item 2 in this grid:
<Grid x:Name="LayoutRoot">
<controls:Panorama Title="Title" SelectionChanged="Panorama_SelectionChanged" FontSize="20">
<!--Panorama item one-->
<controls:PanoramaItem Header="Wall" Margin="40,0,0,0" Name="PIWall">
</controls:PanoramaItem>
<!--Panorama item two-->
<controls:PanoramaItem Header="Messages" Margin="30,0,0,0" Name="PIMail">
</controls:PanoramaItem>
</controls:Panorama>
</Grid>
The button and it’s settings are as follows:
HyperlinkButton btn = new HyperlinkButton();
btn.Height = 89;
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
btn.VerticalAlignment = System.Windows.VerticalAlignment.Top;
btn.Margin = new Thickness(60, -70, 0, 0);
btn.Width = 290;
btn.Content = message;
btn.FontSize = 22;
btn.Visibility = System.Windows.Visibility.Visible;
btn.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
panel.Children.Add(btn);
scrollViewer.Content = panel;
PIMail.Content = mailscrollViewer;
How can i make the text not slide over the grid margins?
OK, to wrap text inside a button, you could use XAML like this:
In order to accomplish this in code, you could do the following:
ContentPanel is a simple grid.
EDIT: Modified post for HyperlinkButton
In order to enable text wrapping in hyperlink button you need to edit the style of the hyperlink button. Define it like this in the App.xaml file:
The important change is that the TextBlock now contains the TextWrapping property set to “Wrap”.
Then use the button like you normally do, just don’t forget to set the styling: