示例
此示例演示如何创建 FontSizeConverter 的实例,并用它来更改字号。
此示例定义一个名为 changeSize 的自定义方法,该方法会将 ListBoxItem(在单独的Extensible Application Markup Language (XAML) 文件中定义)的内容转换为 Double 的实例,然后再转换为 String。 此方法将 ListBoxItem 传递给 FontSizeConverter 对象,该对象将 ListBoxItem 的 Content 转换为 Double 的实例。 然后,此值作为 TextBlock 元素的 FontSize 属性值进行回传。
此示例还定义了名为 changeFamily 的第二个自定义方法。 此方法将 ListBoxItem 的 Content 转换为 String,然后将该值回传给 TextBlock 元素的 FontFamily 属性。
此示例不运行。
Private Sub changeSize(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myFontSizeConverter As System.Windows.FontSizeConverter = New System.Windows.FontSizeConverter()
    text1.FontSize = CType(myFontSizeConverter.ConvertFromString(li.Content.ToString()), Double)
End Sub
Private Sub changeFamily(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
    Dim li2 As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    text1.FontFamily = New System.Windows.Media.FontFamily(li2.Content.ToString())
End Sub
        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());
        }