views:

585

answers:

1

I am trying to use an IFormatProvider to customize some databindings; however the IFormatProvider class is never being called. I put breakpoints at the begining of both functions in my custom formating class and neither are being hit through databinding. When I use my custom formating class with String.Format it works.

I am using .Net 2.0 and winforms.

This is how I do the data bindings:

label1.DataBindings.Add("Text", textBox1, "Text", true, 
                            DataSourceUpdateMode.OnPropertyChanged, 
       "<NULL>","{0:H}",new MyFormat());

This is how I used String.Format:

string test =(string.Format(_superFormat, "{0}", "this is my arg"));

And this is my custom formating class:

    class MyFormat : IFormatProvider, ICustomFormatter
    {
     string ICustomFormatter.Format(string format, object arg, IFormatProvider formatProvider)
     {
      string result = ((string)arg).ToUpper();
      return result ;
     }
     object IFormatProvider.GetFormat(Type formatType)
     {
      if (formatType == typeof(ICustomFormatter))
       return this;
      else
       return null;
     }
    }
+1  A: 

What exactly are you trying to do?

Assuming the Text property is a string, then as far as I know it can't use a formatter, because string doesn't implement IFormattable.

The Binding class (which underpins DataBindings.Add) has a Format event and Parse event that can be used to control formatting. You can also use a TypeConverter on the target bound property, which I like because it moves this logic away from the UI.

So: do you have an example of what you want to do?

Marc Gravell