Programmatically change the FontFamily property of a TextBlock element. : Font « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Font 
24.92.5.Programmatically change the FontFamily property of a TextBlock element.
<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.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Media;

namespace FontSizeConverter_Csharp
{
  public partial class Window1 : Window
  {
        public void changeSize(object sender, SelectionChangedEventArgs args)
        {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
      FontSizeConverter myFontSizeConverter = new FontSizeConverter();
      text1.FontSize = (Double)myFontSizeConverter.ConvertFromString(li.Content.ToString());
        }

    public void changeFamily(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            text1.FontFamily = new System.Windows.Media.FontFamily(li2.Content.ToString());
        }
  }
}
WPF Programmatically Change The Font Family Property Of A Text Block Element
24.92.Font
24.92.1.Font Properties MovedFont Properties Moved
24.92.2.Animate TextBox Font SizeAnimate TextBox Font Size
24.92.3.Create FishEye Effect Buttons by changing the Button font sizeCreate FishEye Effect Buttons by changing the Button font size
24.92.4.Font ViewerFont Viewer
24.92.5.Programmatically change the FontFamily property of a TextBlock element.Programmatically change the FontFamily property of a TextBlock element.
24.92.6.Font Properties SampleFont Properties Sample
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.