winforms 有办法转换绑定数据成员吗?

zmeyuzjn  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

尝试将dataMember从int转换为Color

color1.DataBindings.Add("BackColor",NavBill.DataSource,"Color1" );

字符串
Control:color1 is panel绑定dataMember:“Color1”is columns(int,null)
如何将dataMemer的值从int转换为Color,然后再赋值给property?

jtw3ybtb

jtw3ybtb1#

微软的ColorConverter只将Color转换为String(具有人类可读的名称)。
看起来你应该编写自己的转换方法,将整数转换为Color并返回。你需要两个实现ConvertEventHandler的方法。
然后,您可以使用以下代码将它们附加到绑定中:

var b = new Binding("BackColor",NavBill.DataSource,"Color1" );
b.Format += ConvertEventHandler(IntegerColumnToColor); // The parameter to the constructor here is your own method.
b.Parse += ConvertEventHandler(ColorToIntegerColumn); // You might not need this handler.
color1.DataBindings.Add(b);

字符串
您可以只编写IntegerColumnToColor方法并将其添加到处理Format中。

相关问题