本文介绍了将多个值绑定到一个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的示例中,我填写了两个标签白色SQL数据。
效果很好!我问自己,只是想知道是否也可以在单个标签中绑定info1和info2,例如,在label1中加载?
In the example below, I fill two labels white SQL data.
This works well! I asked myself, just wondering if it is also possible to binding info1 and info2 in a single label, for example, load in label1?
If (reader.Read())
{
Label1.text = convert.ToString(reader["info1"]);
Label2.text = convert.ToString(reader["info2"]);
}
推荐答案
Label1.text = Convert.ToString(reader["info1"])+"-"+Convert.ToString(reader["info2"]);
OR
OR
Label1.text = convert.ToString(reader["info1"]);
Label1.text += convert.ToString(reader["info2"]);
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace YourNamespace
{
public class BindLabel : INotifyPropertyChanged
{
private string _labelText;
// for testing only
private Dictionary<string,> reader = new Dictionary<string,>
{
{"info1",99}, {"info2",34}
};
public string LabelText
{
get { return _labelText; }
set
{
_labelText =
string.Format("{0} : {1}",
Convert.ToString(reader["info1"]),
Convert.ToString(reader["info2"]));
OnPropertyChanged(new PropertyChangedEventArgs("LabelText"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
}
用法:
BindLabel bindingSource = new BindLabel();
label1.DataBindings.Add("Text",bindingSource,"LabelText");
label1.Text = "IgnoreThis";
问题是:您是否真的想为通过绑定处理的简单字符串连接编写此类代码?
The question is: do you really want to write such code for a simple string concatenation to be handled through binding ?
这篇关于将多个值绑定到一个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!