Faisal Ashfaq
Faisal Ashfaq

Reputation: 2678

Content of button adapts size according to button?

I want to resize content within a button so that when I "resize" my window, buttons also gets resized but size of content within button remains unchanged?

What should I do in order to make contents resized WRT button size.

Thanks, Faisal

PS: I am using GRID layout in WPF. enter image description here

Upvotes: 1

Views: 135

Answers (2)

Sheridan
Sheridan

Reputation: 69987

The easiest way that I can think of you doing that is for you to use a ViewBox element inside your Buttons:

<Button>
    <ViewBox>
        <!--Your Button content--> 
    </ViewBox>
</Button>

Upvotes: 3

kosdos
kosdos

Reputation: 368

I believe the best approach to this issue would be to actually make a tiny converter which would be dynamically controlling the text size.

Here is an example of such a converter:

C#

using System.Globalization;

public class FontSizeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double actualHeight = System.Convert.ToDouble(value);
        int fontSize = (int)(actualHeight * .5);
        return fontSize;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And then here is the usage of this class within your xaml:

XAML

...
<Window.Resources>
    <l:FontSizeConverter x:Key="FSConverter" />
</Window.Resources>
...
<Grid>   
    <Button Content="Dat Button" FontSize="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Converter={StaticResource FSConverter}}"/>    
</Grid>

Hope this helps, let me know if you will have any issues with that.

K.

Upvotes: 0

Related Questions