none
How to apply Font Family and Font Size to selected text in rich text box

    Question

  • Hi,

       I have two drop down, one is Font Family, here i loaded all the font family, next one is Font Size, here i load some numbers and i have one rich text box, in the rich text box i will select some text then i will choose some font family and font size, based on that selection my rich text box's text font size and font family should be change. How i can achieve this feature, here i attached my sample code

    <Window x:Class="Conversation_Window.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:med="clr-namespace:System.Windows.Media;assembly=PresentationCore"
      Title="Window1" Height="300" Width="500">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="30"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <StackPanel Orientation="Horizontal" Grid.Row="0">
          <TextBlock Text="Font Family" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontFamily" Margin="5,0,5,0" MinWidth="150" ItemsSource="{Binding Source={x:Static Member=med:Fonts.SystemFontFamilies}}"/>
          <TextBlock Text="Font Size" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontSize" MinWidth="50">
            <ComboBoxItem Content="8"/>
            <ComboBoxItem Content="9"/>
            <ComboBoxItem Content="10"/>
            <ComboBoxItem Content="11"/>
            <ComboBoxItem Content="12"/>        
          </ComboBox>
        </StackPanel>
        
        <StackPanel Orientation="Horizontal" Grid.Row="1">
          <RichTextBox Width="300" Height="70" Margin="0,0,0,160"/>      
        </StackPanel>
      </Grid>
    </Window>
    
    Tuesday, March 22, 2011 1:09 PM

Answers

  • Here you go. Changed your XAML a little bit.

    <Window x:Class="RTBSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:med="clr-namespace:System.Windows.Media;assembly=PresentationCore"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="30"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    
        <StackPanel Orientation="Horizontal" Grid.Row="0">
          <TextBlock Text="Font Family" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontFamily" Margin="5,0,5,0" MinWidth="150" 
            ItemsSource="{Binding Source={x:Static Member=med:Fonts.SystemFontFamilies}}"/>
          <TextBlock Text="Font Size" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontSize" MinWidth="50">
            <sys:Double>8</sys:Double>
            <sys:Double>10</sys:Double>
            <sys:Double>14</sys:Double>
            <sys:Double>20</sys:Double>
            <sys:Double>26</sys:Double>
          </ComboBox>
        </StackPanel>
    
        <StackPanel Orientation="Horizontal" Grid.Row="1">
          <RichTextBox x:Name="richTextBox" Width="300" Height="70" Margin="0,0,0,160"/>
        </StackPanel>
      </Grid>
    </Window>
    
    

    using System.Windows;
    using System.Windows.Documents;
    
    namespace RTBSelection
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          cmbFontFamily.SelectionChanged += (s, e) =>
            richTextBox.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, e.AddedItems[0]);
    
          cmbFontSize.SelectionChanged += (s, e) =>
            richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, e.AddedItems[0]);
        }
      }
    }
    
    


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsbyspot.wordpress.com
    Tuesday, March 22, 2011 5:58 PM

All replies

  • Use RichTextBox.Selection ApplyPropertyValue method.
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsbyspot.wordpress.com
    Tuesday, March 22, 2011 3:36 PM
  • Hi Bigsby,

    Can you explain with good example

    Tuesday, March 22, 2011 5:19 PM
  • Here you go. Changed your XAML a little bit.

    <Window x:Class="RTBSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:med="clr-namespace:System.Windows.Media;assembly=PresentationCore"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="30"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    
        <StackPanel Orientation="Horizontal" Grid.Row="0">
          <TextBlock Text="Font Family" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontFamily" Margin="5,0,5,0" MinWidth="150" 
            ItemsSource="{Binding Source={x:Static Member=med:Fonts.SystemFontFamilies}}"/>
          <TextBlock Text="Font Size" HorizontalAlignment="Center" VerticalAlignment="Center"/>
          <ComboBox Name="cmbFontSize" MinWidth="50">
            <sys:Double>8</sys:Double>
            <sys:Double>10</sys:Double>
            <sys:Double>14</sys:Double>
            <sys:Double>20</sys:Double>
            <sys:Double>26</sys:Double>
          </ComboBox>
        </StackPanel>
    
        <StackPanel Orientation="Horizontal" Grid.Row="1">
          <RichTextBox x:Name="richTextBox" Width="300" Height="70" Margin="0,0,0,160"/>
        </StackPanel>
      </Grid>
    </Window>
    
    

    using System.Windows;
    using System.Windows.Documents;
    
    namespace RTBSelection
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          cmbFontFamily.SelectionChanged += (s, e) =>
            richTextBox.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, e.AddedItems[0]);
    
          cmbFontSize.SelectionChanged += (s, e) =>
            richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, e.AddedItems[0]);
        }
      }
    }
    
    


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsbyspot.wordpress.com
    Tuesday, March 22, 2011 5:58 PM
  • Hi Bigsby,

    Thank you again.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 8:24 AM
    Moderator