c# – 将多个DataBindings添加到Winforms标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 将多个DataBindings添加到Winforms标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1231字,纯文字阅读大概需要2分钟。
内容图文
![c# – 将多个DataBindings添加到Winforms标签](/upload/InfoBanner/zyjiaocheng/804/df3efbac7d29498e82a6d5802524c028.jpg)
我在绑定到DataGridView的BindingSource的Label控件中显示不同的值,具体取决于所选的行.
现在,标签有一个数据绑定,如下所示:
this.label9.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bev_BindingSource, "countryRegion", true));
它成功显示countryRegion列的值.
是否可以将数据绑定设置为两列的连接结果,在本例中为countryRegion和country?
解决方法:
您需要使用MultiBinding.添加两个Binding实例(一个用于countryRegion,一个用于country.然后您需要实现一个IMultiValueConverter,它将接受两个绑定对象生成的值并将它们转换为单个值.
绑定设置看起来像这样:
var multiBinding = new MultiBinding();
multiBinding.Bindings.Add(new Binding("Text", this.bev_BindingSource, "countryRegion", true));
multiBinding.Bindings.Add(new Binding("Text", this.bev_BindingSource, "country", true));
multiBinding.Converter = new MyMultiValueConverter();
this.label9.DataBindings.Add(multiBinding);
转换器实现看起来像:
public class MyMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// perform your conversion here and return the final value
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
内容总结
以上是互联网集市为您收集整理的c# – 将多个DataBindings添加到Winforms标签全部内容,希望文章能够帮你解决c# – 将多个DataBindings添加到Winforms标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。