使用例
この例では、FontSizeConverter のインスタンスを作成し、それを使用してフォント サイズを変更する方法を説明します。
この例では、changeSize という名前のカスタム メソッドを定義します。このメソッドは、別の Extensible Application Markup Language (XAML) ファイルで定義されている ListBoxItem のコンテンツを、まず Double のインスタンスに変換し、その後で String に変換します。 このメソッドは ListBoxItem を FontSizeConverter オブジェクトに渡します。このオブジェクトは、ListBoxItem の Content を Double のインスタンスに変換します。 その後、この値は、TextBlock 要素の FontSize プロパティの値として戻されます。
また、この例では、changeFamily という名前の第 2 のカスタム メソッドも定義されています。 このメソッドは、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());
}