locked
How To Change Font of part of String in Windows 8 phone in c# RRS feed

  • Question

  • Input- my name is manas.

    Output- my NAME is Manas.



    Is it possible in ListBox
    • Edited by manas256 Saturday, July 4, 2015 12:01 PM
    Saturday, July 4, 2015 10:37 AM

Answers

  • A string itself doesn't have any specific font but you could put several Run elements inside a TextBlock to make a part of the text bold:

    <TextBlock>
                <Run>my name is </Run>
                <Run FontWeight="Bold">Manas</Run>
            </TextBlock>

    <TextBlock x:Name="txt"/>

    txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "my name is " });
    txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "Manas", FontWeight = Windows.UI.Text.FontWeights.Bold });


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Fred Bao Wednesday, July 15, 2015 6:00 AM
    • Marked as answer by Jamles Hez Wednesday, July 15, 2015 1:01 PM
    Saturday, July 4, 2015 10:44 AM
  • Please only ask one question per thread. If you have a new question you should start a new thread.

    >>Is it possible to access a textblock from listbox and change font of string as above.

    The easiest way to do this is probably to handle the Loaded event of the TextBlock in the ItemTemplate of the ListBox:

            <ListBox>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="txt" Loaded="txt_Loaded"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>


    private void tb_Loaded(object sender, RoutedEventArgs e)
            {
                TextBox txt = sender as TextBox;
                txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "my name is " });
                txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "Manas", FontWeight = Windows.UI.Text.FontWeights.Bold });
            }

    Hope that helps.

    Please remember to mark all helpful posts as answer to close the thread and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Jamles Hez Wednesday, July 15, 2015 1:01 PM
    Monday, July 6, 2015 9:29 AM

All replies

  • A string itself doesn't have any specific font but you could put several Run elements inside a TextBlock to make a part of the text bold:

    <TextBlock>
                <Run>my name is </Run>
                <Run FontWeight="Bold">Manas</Run>
            </TextBlock>

    <TextBlock x:Name="txt"/>

    txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "my name is " });
    txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "Manas", FontWeight = Windows.UI.Text.FontWeights.Bold });


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Fred Bao Wednesday, July 15, 2015 6:00 AM
    • Marked as answer by Jamles Hez Wednesday, July 15, 2015 1:01 PM
    Saturday, July 4, 2015 10:44 AM
  • Thanks a lot .

    Is it possible to access a textblock from listbox and  change font of string as above.

    Monday, July 6, 2015 5:46 AM
  • Please only ask one question per thread. If you have a new question you should start a new thread.

    >>Is it possible to access a textblock from listbox and change font of string as above.

    The easiest way to do this is probably to handle the Loaded event of the TextBlock in the ItemTemplate of the ListBox:

            <ListBox>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="txt" Loaded="txt_Loaded"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>


    private void tb_Loaded(object sender, RoutedEventArgs e)
            {
                TextBox txt = sender as TextBox;
                txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "my name is " });
                txt.Inlines.Add(new Windows.UI.Xaml.Documents.Run() { Text = "Manas", FontWeight = Windows.UI.Text.FontWeights.Bold });
            }

    Hope that helps.

    Please remember to mark all helpful posts as answer to close the thread and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Jamles Hez Wednesday, July 15, 2015 1:01 PM
    Monday, July 6, 2015 9:29 AM