Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据绑定。使用IFormatProvider添加_C#_.net_Data Binding - Fatal编程技术网

C# 数据绑定。使用IFormatProvider添加

C# 数据绑定。使用IFormatProvider添加,c#,.net,data-binding,C#,.net,Data Binding,我试图使用IFormatProvider定制一些数据绑定;但是,永远不会调用IFormatProvider类。在我的自定义格式化类中,我在两个函数的开始处都设置了断点,并且两个断点都没有通过数据绑定被命中。当我使用带有String.Format的自定义格式化类时,它会起作用 我正在使用.NET2.0和winforms 以下是我执行数据绑定的方式: label1.DataBindings.Add("Text", textBox1, "Text", true,

我试图使用IFormatProvider定制一些数据绑定;但是,永远不会调用IFormatProvider类。在我的自定义格式化类中,我在两个函数的开始处都设置了断点,并且两个断点都没有通过数据绑定被命中。当我使用带有String.Format的自定义格式化类时,它会起作用

我正在使用.NET2.0和winforms

以下是我执行数据绑定的方式:

label1.DataBindings.Add("Text", textBox1, "Text", true, 
                            DataSourceUpdateMode.OnPropertyChanged, 
                "<NULL>","{0:H}",new MyFormat());
这是我的自定义格式化类:

    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;
        }
    }

你到底想干什么

假设
Text
属性是一个字符串,那么据我所知,它不能使用格式化程序,因为
string
没有实现
IFormattable

绑定
类(支持
数据绑定.Add
)有一个事件和一个事件,可用于控制格式。您还可以在target-bound属性上使用
TypeConverter
,我喜欢它,因为它将此逻辑从UI中移开


那么:你有一个你想做什么的例子吗?

他的问题很清楚。他为databings.Add提供了一个自定义格式化程序,WinForms完全忽略了它,从不调用GetFormat()或Format()。我也有同样的问题,不知道为什么在没有实际使用格式化程序时可以指定格式化程序。
    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;
        }
    }