locked
Setting the FontWeight of Textblock in C# RRS feed

  • General discussion

  • How to set the fontweight in the c# code in Metrostyle apps

    Sivalingam

    Friday, July 27, 2012 8:02 AM

All replies

  • Friday, July 27, 2012 8:11 AM
  • Thts right CanBilgin,

    I need to set the specfied text in c# code ?


    Sivalingam

    Friday, July 27, 2012 9:45 AM
  • Thats what I got it.

    textblock.Inlines.Add((new Run() {Text = text1[i], FontWeight = FontWeights.Bold}));


    Sivalingam

    Friday, July 27, 2012 11:17 AM
  • Here is an example to illustrate Can Bilgin's answer.

    Possibly the problem initially arose because you might need to set "ExtraBold/Light" to really perceive the difference which admittedly is not spectacular.

    <Page
        x:Class="App1.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBlock x:Name="myTextBlock1"/>
                <TextBlock x:Name="myTextBlock2"/>
                <TextBlock x:Name="myTextBlock3"/>
                <TextBlock x:Name="myTextBlock4"/>
                <TextBlock x:Name="myTextBlock5"/>
                <Button Content="Add" Click="Button_Click_1"/>
            </StackPanel>
        </Grid>
    </Page>

    ---

    using Windows.UI.Text;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Documents;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace App1
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
    
            public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = this;
            }
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                bool tryBigSize = true; // just an example to accentuate through 'Size'
                if (tryBigSize)
                {
                    myTextBlock1.Inlines.Add((new Run() { Text = "Aaa", FontSize = 24, FontWeight = FontWeights.ExtraBold }));
                    myTextBlock2.Inlines.Add((new Run() { Text = "Aaa", FontSize = 24, FontWeight = FontWeights.Bold }));
                    myTextBlock3.Inlines.Add((new Run() { Text = "Aaa", FontSize = 24, FontWeight = FontWeights.Normal }));
                    myTextBlock4.Inlines.Add((new Run() { Text = "Aaa", FontSize = 24, FontWeight = FontWeights.Light }));
                    myTextBlock5.Inlines.Add((new Run() { Text = "Aaa", FontSize = 24, FontWeight = FontWeights.ExtraLight }));
                }
                else
                {
                    myTextBlock1.Inlines.Add((new Run() { Text = "Aaa", FontWeight = FontWeights.ExtraBold })); // accentuate true 'Weight'
                    myTextBlock2.Inlines.Add((new Run() { Text = "Aaa", FontWeight = FontWeights.Bold }));
                    myTextBlock3.Inlines.Add((new Run() { Text = "Aaa", FontWeight = FontWeights.Normal }));
                    myTextBlock4.Inlines.Add((new Run() { Text = "Aaa", FontWeight = FontWeights.Light }));
                    myTextBlock5.Inlines.Add((new Run() { Text = "Aaa", FontWeight = FontWeights.ExtraLight }));
                }
            }
        }
    }
    

    Friday, July 27, 2012 12:48 PM