【发布时间】:2015-04-25 12:47:12
【问题描述】:
我无法理解为什么我的数据绑定似乎不适用于我的自定义类。我让(破解)我的类扩展了 Control 类以添加数据绑定功能,但它实际上并没有绑定到我的自定义属性。
我的自定义类的代码是:
public class RadioButtonSet : System.Windows.Forms.Control
{
private Dictionary<System.Windows.Forms.RadioButton, int> buttonList;
private int selectedValue;
public RadioButtonSet()
{
buttonList = new Dictionary<System.Windows.Forms.RadioButton, int>();
}
public void AddButton(System.Windows.Forms.RadioButton button, int buttonValue)
{
if (this.buttonList.ContainsKey(button))
throw new Exception("Button set already contains specified button");
else if (buttonValue <= 0)
throw new Exception("Cannot add specified key to button set");
else if (button == null)
throw new Exception("Parameter button cannot be null");
else
{
button.CheckedChanged += button_CheckedChanged;
this.buttonList.Add(button, buttonValue);
}
}
private void setSelectedButton()
{
this.buttonList.FirstOrDefault(x => x.Value == this.selectedValue).Key.Checked = true;
}
private void button_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton btn = sender as System.Windows.Forms.RadioButton;
this.selectedValue = this.buttonList[btn];
}
public int SelectedButton
{
get
{
return selectedValue;
}
set
{
selectedValue = value;
setSelectedButton();
}
}
}
我尝试使用以下内容绑定到此类,其中 rbs_admin 是我的自定义类的一个实例:
rbs_admin.DataBindings.Add("SelectedButton", datatable, "admin");
我不知道哪些信息可能会有所帮助。
我从由数据适配器填充的数据表中获取要绑定的信息。这个自定义类不在它自己的文件中,它是我项目中另一个静态类的一部分。
我只是不明白,因为我创建了一个具有相同自定义属性的自定义文本框,它绑定并正常工作。
非常感谢任何帮助。
【问题讨论】:
-
您的绑定是正确的,但您错过了这样一个事实,即您需要通过某种方式从数据表中设置选定的行,以便用户控件更新其绑定。
标签: c# winforms data-binding binding