我有一个组合框,其中SelectedItem
是数据绑定的。我想在控件上显示不同的值,这些值来自我在模型中存储的内容(显示:“逗号”,型号:“,”)。要在两个我为Binding.Format
和Binding.Parse
实现的处理程序之间进行转换
// simplified for test case purposes
binding.Format += (s, e) => e.Value = ((string)e.Value == ",") ? "Comma" : "Semicolon";
binding.Parse += (s, e) => e.Value = ((string)e.Value == "Comma") ? "," : ";";
:
Format
当程序运行时,我希望组合框显示“逗号”,但它 没有显示任何内容(作为其选择的项目)。
我做错了什么?
文本框(具有类似的映射)显示正确的值。
没有映射(即用模型中的“逗号”替换“,”) 删除处理程序)组合框显示预期值。
我注意到(使用断点)Format
处理程序运行两次
表格显示,为什么它运行两次(而不是一次)?
表单关闭后,public class Model
{
public string Value { get; set; } = ",";
}
[STAThread]
static void Main()
{
var model = new Model();
var comboBox = new ComboBox() { Dock = DockStyle.Top };
comboBox.Items.AddRange(new object[] { "Comma", "Semicolon", });
var binding = new Binding(nameof(ComboBox.SelectedItem), model, nameof(Model.Value));
binding.Format += (s, e) => e.Value = ((string)e.Value == ",") ? "Comma" : "Semicolon";
binding.Parse += (s, e) => e.Value = ((string)e.Value == "Comma") ? "," : ";";
comboBox.DataBindings.Add(binding);
var textBox = new TextBox() { Dock = DockStyle.Top };
var binding1 = new Binding(nameof(TextBox.Text), model, nameof(Model.Value));
binding1.Format += (s, e) => e.Value = ((string)e.Value == ",") ? "Comma" : "Semicolon";
binding1.Parse += (s, e) => e.Value = ((string)e.Value == "Comma") ? "," : ";";
textBox.DataBindings.Add(binding1);
Application.Run(new Form() { Controls = { comboBox, textBox, } });
}
处理程序再次运行两次,为什么会这样
什么都跑?
Session::forget('key')