Programmatically change the FontFamily property of a TextBlock element. : Font « Windows Presentation Foundation « VB.Net Tutorial






<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="FontSizeConverter_Csharp.Window1"
    Title="FontSizeConverter Sample">
<StackPanel>
        <TextBlock Name="text1" TextWrapping="Wrap" Height="300" Width="500">
            this is a test
        </TextBlock>
    <ListBox Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeSize">
        <ListBoxItem>5</ListBoxItem>
        <ListBoxItem>8</ListBoxItem>
        <ListBoxItem>11</ListBoxItem>
        <ListBoxItem>14</ListBoxItem>
        <ListBoxItem>17</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
        <ListBoxItem>23</ListBoxItem>
        <ListBoxItem>26</ListBoxItem>
        <ListBoxItem>29</ListBoxItem>
        <ListBoxItem>32</ListBoxItem>
    </ListBox>
    <ListBox Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeFamily">
      <ListBoxItem>Arial</ListBoxItem>
      <ListBoxItem>Courier New</ListBoxItem>
      <ListBoxItem>Tahoma</ListBoxItem>
      <ListBoxItem>Times New Roman</ListBoxItem>
      <ListBoxItem>Verdana</ListBoxItem>
    </ListBox>      
   
  </StackPanel>        
</Window>

//File:Window.xaml.vb

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Navigation
Imports System.Windows.Media

Namespace FontSizeConverter_Csharp
  Public Partial Class Window1
    Inherits Window
    Public Sub changeSize(sender As Object, args As SelectionChangedEventArgs)
      Dim li As ListBoxItem = TryCast(TryCast(sender, ListBox).SelectedItem, ListBoxItem)
      Dim myFontSizeConverter As New FontSizeConverter()
      text1.FontSize = CType(myFontSizeConverter.ConvertFromString(li.Content.ToString()), [Double])
    End Sub

    Public Sub changeFamily(sender As Object, args As SelectionChangedEventArgs)
      Dim li2 As ListBoxItem = TryCast(TryCast(sender, ListBox).SelectedItem, ListBoxItem)
      text1.FontFamily = New System.Windows.Media.FontFamily(li2.Content.ToString())
    End Sub
  End Class
End Namespace
WPF Programmatically Change The Font Family Property Of A Text Block Element








16.74.Font
16.74.1.FontSize unitsFontSize units
16.74.2.Change a Control's Font Weight on Mouse OverChange a Control's Font Weight on Mouse Over
16.74.3.Display Simple Text with TextBlock and set font, alignmentDisplay Simple Text with TextBlock and set font, alignment
16.74.4.Section Font StyleSection Font Style
16.74.5.Adding TextBlock to StackPanel with different font sizeAdding TextBlock to StackPanel with different font size
16.74.6.Using text format property aliasesUsing text format property aliases
16.74.7.Programmatically change the FontFamily property of a TextBlock element.Programmatically change the FontFamily property of a TextBlock element.
16.74.8.Font Properties SampleFont Properties Sample