How to: Use the FontSizeConverter Class

Example

This example shows how to create an instance of FontSizeConverter and use it to change a font size.

The example defines a custom method called changeSize that converts the contents of a ListBoxItem, as defined in a separate Extensible Application Markup Language (XAML) file, to an instance of Double, and later into a String. This method passes the ListBoxItem to a FontSizeConverter object, which converts the Content of a ListBoxItem to an instance of Double. This value is then passed back as the value of the FontSize property of the TextBlock element.

This example also defines a second custom method that is called changeFamily. This method converts the Content of the ListBoxItem to a String, and then passes that value to the FontFamily property of the TextBlock element.

This example does not run.

private 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());
}

private void changeFamily(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
    text1.FontFamily = new System.Windows.Media.FontFamily(li2.Content.ToString());
}

See also