@MERUN KUMAR MAITY, Welcome to Microsoft Q&A, you could try the following code to scale the textblock inside the button with MainWindow.
First, please create two custom Converter classes.
public class WidthToScaleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double width = (double)value;
return width / 100.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class HeightToScaleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double height = (double)value;
return height / 50.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Second, please add the following xaml code in App.xaml
.
<Application.Resources>
<local:WidthToScaleConverter x:Key="WidthToScaleConverter"/>
<local:HeightToScaleConverter x:Key="HeightToScaleConverter"/>
</Application.Resources>
Third, please use the following XAML code to reduce the Text size without effecting Font Size 10.
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Name="myMainWindow">
<Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged">
<Grid.LayoutTransform>
<ScaleTransform x:Name="ApplicationScaleTransform"
CenterX="0"
CenterY="0"
ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
</Grid.LayoutTransform>
<Grid VerticalAlignment="Center" HorizontalAlignment="Center" Height="150">
<Button VerticalAlignment="Center" HorizontalAlignment="Center" Height="80" Width="180">
<Button.LayoutTransform>
<ScaleTransform ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
</Button.LayoutTransform>
<!-- Embedding TextBlock via Button.Content -->
<Button.Content>
<!-- Binding FontSize to ScaleValue with a multiplier -->
<TextBlock x:Name="myTextBlock"
Text="Settings"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10">
<TextBlock.LayoutTransform>
<!-- ScaleTransform will change the size of the TextBlock without affecting the font size -->
<ScaleTransform ScaleX="{Binding ActualWidth, ElementName=myButton, Converter={StaticResource WidthToScaleConverter}}"
ScaleY="{Binding ActualHeight, ElementName=myButton, Converter={StaticResource HeightToScaleConverter}}"/>
</TextBlock.LayoutTransform>
</TextBlock>
</Button.Content>
</Button>
</Grid>
</Grid>
</Window>
Note: I used the same code with you in MainWindow.cs.
Tested Result:
As you can see from the above picture, fontsize will be changed by the window size.
Hope it could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.