使用其他文本在C#中对标签进行数据绑定? [英] Databinding a label in C# with additional text?

查看:62
本文介绍了使用其他文本在C#中对标签进行数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来对标签进行数据绑定并包括一些自定义文本?

Is there an easy way to databind a label AND include some custom text?

当然我可以像这样绑定标签:

Of course I can bind a label like so:

someLabel.DataBindings.Add(new Binding( Text,this.someBindingSource, SomeColumn,true));

someLabel.DataBindings.Add(new Binding("Text", this.someBindingSource, "SomeColumn", true));

但是我会添加自定义文本,以便结果是这样的:
someLabel.Text =自定义文本 + databoundColumnText;

But how would I add custom text, so that the result would be something like: someLabel.Text = "Custom text " + databoundColumnText;

我真的必须求助于自定义代码...?

Do I really have to resort to custom code...?

(也许我的头因感冒而迷雾了,我看不到简单的解决方案?)

(maybe my head is too fogged from my cold and I can't see a simple solution?)

有关此问题的任何帮助。

TIA for any help on this matter.

推荐答案

您始终可以使用Binding.Format事件

You can always use Binding.Format event.

http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format.aspx


当从中推送数据
时引发Format事件将数据源放入
控件中。您可以处理Format
事件,以将未格式化的数据从
数据源转换为格式化的数据
进行显示。

The Format event is raised when data is pushed from the data source into the control. You can handle the Format event to convert unformatted data from the data source into formatted data for display.

类似...

    private string _bindToValue = "Value from DataSource";
    private string _customText = "Some Custom Text: ";
    private void Form1_Load(object sender, EventArgs e)
    {
        var binding = new Binding("Text",_bindToValue,null);
        binding.Format += delegate(object sentFrom, ConvertEventArgs convertEventArgs)
                              {
                                  convertEventArgs.Value = _customText + convertEventArgs.Value;
                              };

        label1.DataBindings.Add(binding);
    }

这篇关于使用其他文本在C#中对标签进行数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆