Convert and ConvertBack Methods

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>
<src:DoubleToStringConverter 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
 
P

Peter Duniho

Gordon said:
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 think the basic problem is that you misunderstand the use of the Path
attribute. It works in the IconFontFamily case, because a FontFamily
object actually has a Source property. But when you specify "Source" as
the Path for the binding to the IconFontSize property, which returns a
double, that binding fails because there's no Source property in the
System.Double struct.

I wish WPF would just throw an exception when you screw up the binding;
it would make it a lot easier to detect and fix problems like this.
But, you can still find out about errors by showing the Output window
during debugging. Error messages from data binding will show up there.
Had you looked, you would have seen an error related to the failure to
find the Source property on the Double struct.

Just leave off the Path. You want the value from the Source object
itself, rather than some property in the Source object. So there's no
need to specify a Path value in either case.

Beyond that you should not need a converter at all, because the default
is to simply use the ToString() return value, which is all your
converter does anyway.

Pete
 
G

Gordon Padwick

Thanks for the help and for so clearly explaining the problem. I followed
your suggestion and everything works fine.

Gordon

Peter Duniho said:
Gordon said:
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 think the basic problem is that you misunderstand the use of the Path
attribute. It works in the IconFontFamily case, because a FontFamily
object actually has a Source property. But when you specify "Source" as
the Path for the binding to the IconFontSize property, which returns a
double, that binding fails because there's no Source property in the
System.Double struct.

I wish WPF would just throw an exception when you screw up the binding; it
would make it a lot easier to detect and fix problems like this. But, you
can still find out about errors by showing the Output window during
debugging. Error messages from data binding will show up there. Had you
looked, you would have seen an error related to the failure to find the
Source property on the Double struct.

Just leave off the Path. You want the value from the Source object
itself, rather than some property in the Source object. So there's no
need to specify a Path value in either case.

Beyond that you should not need a converter at all, because the default is
to simply use the ToString() return value, which is all your converter
does anyway.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top