Ian*_*ose 11 data-binding winforms
在WPF中,很容易使用ValueConverter格式化值等(在我们的例子中,将一些数字转换为不同的单位,例如km到英里)
我知道它可以在Winforms中完成,但我所有的Googleing只会为WPF和Silverlight带来结果.
Ada*_*son 16
如果您能够并愿意使用自定义属性装饰数据源属性,则可以使用TypeConverter.
否则,您必须附加到对象的事件Parse和Format事件Binding.遗憾的是,除了最简单的场景之外,这使得设计师无需使用绑定.
例如,假设您希望TextBox绑定到表示公里数的整数列,并且您希望以英里为单位的可视化表示:
在构造函数中:
Binding bind = new Binding("Text", source, "PropertyName");
bind.Format += bind_Format;
bind.Parse += bind_Parse;
textBox.DataBindings.Add(bind);
Run Code Online (Sandbox Code Playgroud)
...
void bind_Format(object sender, ConvertEventArgs e)
{
int km = (int)e.Value;
e.Value = ConvertKMToMiles(km).ToString();
}
void bind_Parse(object sender, ConvertEventArgs e)
{
int miles = int.Parse((string)e.Value);
e.Value = ConvertMilesToKM(miles);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4899 次 |
| 最近记录: |