gpt4 book ai didi

c# - DataBindings.Add 使用 IFormatProvider

转载 作者:行者123 更新时间:2023-11-30 16:36:28 25 4
gpt4 key购买 nike

我正在尝试使用 IFormatProvider 来自定义一些数据绑定(bind);但是,永远不会调用 IFormatProvider 类。我在我的自定义格式类中的两个函数的开头都放置了断点,并且都没有通过数据绑定(bind)命中。当我将我的自定义格式化类与 String.Format 一起使用时,它会起作用。

我正在使用 .Net 2.0 和 winforms。

这是我进行数据绑定(bind)的方式:

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

这就是我使用 String.Format 的方式:

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

这是我的自定义格式类:

    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 没有t 实现 IFormattable

Binding 类(DataBindings.Add 的基础)有一个 Format事件和 Parse可用于控制格式的事件。您还可以在目标绑定(bind)属性上使用 TypeConverter,我喜欢这样做,因为它将此逻辑从 UI 移开。

那么:您有想要做什么的示例吗?

关于c# - DataBindings.Add 使用 IFormatProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/786903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com