G
Gordon Padwick
I am having problems understanding how to use the Convert and ConvertBack
methods in WPF. Help in Visual Studio 2008 and the various books I have deal
with these methods only superficially.
After reproducing various published examples, I tried to use Convert and
ConvertBack on my own. My project was to display information about one of
the system fonts in TextBlock controls. A TextBlock control, as I understand
it, can display only a string. I had no problem in displaying a font family
name in a TextBox control because something like SystemFonts.IconFontFamily
returns a string.
My problem arose when I tried to display the font size in a TextBlock.
SystemFonts.IconFontSize returns a Double that can't be directly displayed
in a TextBox; it must be converted to a string. That's where I ran into a
problem that I haven't yet been able to solve.
I have three files: Window1.xaml, Window1.xaml.cs, and Converter.cs.
Window1.xaml contains:
<Window x:Class="SystemFonts.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SystemFonts"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<srcoubleToStringConverter x:Key="formatter"/>
</Window.Resources>
<Grid>
<TextBlock Height="21"
Margin="124,0,34,41"
Name="textBlock1"
VerticalAlignment="Bottom"
Text="{Binding Source=
{x:Static SystemFonts.IconFontFamily},
Path=Source}"
Background="White" />
<TextBlock Height="21"
Margin="124,0,34,10"
Name="textBlock2"
VerticalAlignment="Bottom"
Text="{Binding Source={x:Static
SystemFonts.IconFontSize},
Path=Source, Converter={StaticResource formatter}}"
Background="White"/>
</Grid>
</Window>
Window1.xaml.cs contains:
using System;
using System.Windows;
namespace SystemFonts
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
Converter.cs contains:
using System;
using System.Windows.Data;
namespace SystemFonts
{
[ValueConversion(typeof(Double), typeof(String))]
class DoubleToStringConverter : IValueConverter
{
public DoubleToStringConverter() { }
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
Double IconFontSize = (Double)value;
return IconFontSize.ToString();
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
The build works okay, reporting "Build succeeded." When I run it, though, I
see only the first TextBlock with the correct font name. Apparently, the
convert code doesn't properly convert the font size (a Double) to a string
so that it can be displayed in the TextBlock.
Can someone, please, help me solve this problem? I'll also appreciate being
pointed to a good source for thorough information about using the Convert
and ConvertBack methods.
Gordon
methods in WPF. Help in Visual Studio 2008 and the various books I have deal
with these methods only superficially.
After reproducing various published examples, I tried to use Convert and
ConvertBack on my own. My project was to display information about one of
the system fonts in TextBlock controls. A TextBlock control, as I understand
it, can display only a string. I had no problem in displaying a font family
name in a TextBox control because something like SystemFonts.IconFontFamily
returns a string.
My problem arose when I tried to display the font size in a TextBlock.
SystemFonts.IconFontSize returns a Double that can't be directly displayed
in a TextBox; it must be converted to a string. That's where I ran into a
problem that I haven't yet been able to solve.
I have three files: Window1.xaml, Window1.xaml.cs, and Converter.cs.
Window1.xaml contains:
<Window x:Class="SystemFonts.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SystemFonts"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<srcoubleToStringConverter x:Key="formatter"/>
</Window.Resources>
<Grid>
<TextBlock Height="21"
Margin="124,0,34,41"
Name="textBlock1"
VerticalAlignment="Bottom"
Text="{Binding Source=
{x:Static SystemFonts.IconFontFamily},
Path=Source}"
Background="White" />
<TextBlock Height="21"
Margin="124,0,34,10"
Name="textBlock2"
VerticalAlignment="Bottom"
Text="{Binding Source={x:Static
SystemFonts.IconFontSize},
Path=Source, Converter={StaticResource formatter}}"
Background="White"/>
</Grid>
</Window>
Window1.xaml.cs contains:
using System;
using System.Windows;
namespace SystemFonts
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
Converter.cs contains:
using System;
using System.Windows.Data;
namespace SystemFonts
{
[ValueConversion(typeof(Double), typeof(String))]
class DoubleToStringConverter : IValueConverter
{
public DoubleToStringConverter() { }
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
Double IconFontSize = (Double)value;
return IconFontSize.ToString();
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
The build works okay, reporting "Build succeeded." When I run it, though, I
see only the first TextBlock with the correct font name. Apparently, the
convert code doesn't properly convert the font size (a Double) to a string
so that it can be displayed in the TextBlock.
Can someone, please, help me solve this problem? I'll also appreciate being
pointed to a good source for thorough information about using the Convert
and ConvertBack methods.
Gordon