How to set textwrapping in Button
Asked Answered
M

2

6

did a search on internet, could not find the solution. I think I missed something for below code to make the text wrap:

<Button x:Name="btnCustomerAging" Background="Green" BorderBrush="Green" Foreground="White" FontSize="33" HorizontalAlignment="Left" Margin="662,106,0,0" Grid.Row="1" VerticalAlignment="Top" Height="213" Width="238">

            <TextWrapping>  

              Customer Locations

            </TextWrapping>


</Button>
Murmuration answered 4/9, 2013 at 2:54 Comment(0)
M
7

This will work.

<Button x:Name="btnCustomerAging" Background="Green" BorderBrush="Green" Foreground="White" FontSize="33" 
        HorizontalAlignment="Left" Margin="662,106,0,0" Grid.Row="1" VerticalAlignment="Top" Height="213" Width="238">
    <TextBlock Text="Customer Locations" TextWrapping="Wrap" />
</Button>
Magic answered 4/9, 2013 at 3:3 Comment(0)
U
1

You can create your own WrapButton and use it in XAML like this:

<local:WrapButton x:Name="MyButton" Text="Text that will wrap"/>

This is the code for WrapButton:

public sealed class WrapButton : Button
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(WrapButton), new PropertyMetadata(string.Empty));
    public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }

    public WrapButton()
    {
        var textBlock = new TextBlock { TextAlignment = TextAlignment.Center, TextWrapping = TextWrapping.Wrap };
        textBlock.SetBinding(TextBlock.TextProperty, new Binding { Source = this, Path = new PropertyPath("Text") });
        Content = textBlock;
    }
}
Unblessed answered 30/11, 2014 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.