输入值时 ObjectListView 列中的 C# 组合框
C# combobox in ObjectListView column when enter value
我有来自 ObjectListView
库的带有 TreeListView
的 MainForm。
我想在 ValueColumn
(第二列)中输入具有不同 Windows.Forms.Controls
分量的值。
TreeView
(名称为 jsonTreeView)正常显示所有值及其类型。这是基于我自己的 class:
public class DataTreeNode
{
public string Name { get; set; }
public DataTreeNodeType Type { get; set; }
public string Value { get; set; }
public List<DataTreeNode> Children { get; set; }
}
第一列是 Name
,第二列是 Value
,第三列是 Type
。我想为不同类型的值创建不同的输入控件(它在我的 class 中保存为字符串,但是当转换为 json 时,它的解析类似于 Type
值)。
public partial class MainForm :
{
//...
ObjectListView.EditorRegistry.Register(typeof(string), delegate (Object model, OLVColumn column, Object value)
{
var node = model as DataTreeNode;
if(node == null) return new TextBox();
if (column.Index == 1)
{
switch (node.Type)
{
//...
case DataTreeNodeType.Boolean:
var cmbbBool = new ComboBox();
cmbbBool.Items.Add("False");
cmbbBool.Items.Add("True");
return cmbbBool;
case DataTreeNodeType.Str:
return new TextBox();
default:
return new TextBox();
}
}
return new TextBox();
}
//...
}
文档说:
Once the cell editor has been created, it is given the cell’s value via the controls Value property (if it has one and it is writable). If it doesn’t have a writable Value property, its Text property will be set with a text representation of the cells value.
When the user has finished editing the value in the cell, the new value will be written back into the model object (if possible). To get the modified value, the default processing tries to use the Value property again. It that doesn’t work, the Text property will be used instead.
但是当我尝试使用 comboBox 设置任何值时(此控件 HAS Text
属性)return 值是 null
.
我不仅尝试在组合框中添加字符串,还尝试添加自定义和标准 classes - 没有任何反应。
我该怎么做?
我找到了一些解决方案(不太好,但解决了问题)。
在 ObjectListView
库的源代码中,我发现 BooleanCellEditor
class。它继承自 ComboBox
并将值显示为 Boolean
。我在我的解决方案中复制该代码并将值从 bool
更改为 string
.
OLV源代码:
internal class BooleanCellEditor : ComboBox
{
public BooleanCellEditor() {
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.ValueMember = "Key";
ArrayList values = new ArrayList();
values.Add(new ComboBoxItem(false, "False"));
values.Add(new ComboBoxItem(true, "True"));
this.DataSource = values;
}
}
我的源代码:
public class StringBooleanCellEditor : ComboBox
{
public StringBooleanCellEditor()
{
DropDownStyle = ComboBoxStyle.DropDownList;
ValueMember = "Key";
var values = new ArrayList
{
new ComboBoxItem("False", "Ложь"),
new ComboBoxItem("True", "Истина")
};
DataSource = values;
}
}
我重命名了class,它的名字更适合代码。
我有来自 ObjectListView
库的带有 TreeListView
的 MainForm。
我想在 ValueColumn
(第二列)中输入具有不同 Windows.Forms.Controls
分量的值。
TreeView
(名称为 jsonTreeView)正常显示所有值及其类型。这是基于我自己的 class:
public class DataTreeNode
{
public string Name { get; set; }
public DataTreeNodeType Type { get; set; }
public string Value { get; set; }
public List<DataTreeNode> Children { get; set; }
}
第一列是 Name
,第二列是 Value
,第三列是 Type
。我想为不同类型的值创建不同的输入控件(它在我的 class 中保存为字符串,但是当转换为 json 时,它的解析类似于 Type
值)。
public partial class MainForm :
{
//...
ObjectListView.EditorRegistry.Register(typeof(string), delegate (Object model, OLVColumn column, Object value)
{
var node = model as DataTreeNode;
if(node == null) return new TextBox();
if (column.Index == 1)
{
switch (node.Type)
{
//...
case DataTreeNodeType.Boolean:
var cmbbBool = new ComboBox();
cmbbBool.Items.Add("False");
cmbbBool.Items.Add("True");
return cmbbBool;
case DataTreeNodeType.Str:
return new TextBox();
default:
return new TextBox();
}
}
return new TextBox();
}
//...
}
文档说:
Once the cell editor has been created, it is given the cell’s value via the controls Value property (if it has one and it is writable). If it doesn’t have a writable Value property, its Text property will be set with a text representation of the cells value.
When the user has finished editing the value in the cell, the new value will be written back into the model object (if possible). To get the modified value, the default processing tries to use the Value property again. It that doesn’t work, the Text property will be used instead.
但是当我尝试使用 comboBox 设置任何值时(此控件 HAS Text
属性)return 值是 null
.
我不仅尝试在组合框中添加字符串,还尝试添加自定义和标准 classes - 没有任何反应。
我找到了一些解决方案(不太好,但解决了问题)。
在 ObjectListView
库的源代码中,我发现 BooleanCellEditor
class。它继承自 ComboBox
并将值显示为 Boolean
。我在我的解决方案中复制该代码并将值从 bool
更改为 string
.
OLV源代码:
internal class BooleanCellEditor : ComboBox
{
public BooleanCellEditor() {
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.ValueMember = "Key";
ArrayList values = new ArrayList();
values.Add(new ComboBoxItem(false, "False"));
values.Add(new ComboBoxItem(true, "True"));
this.DataSource = values;
}
}
我的源代码:
public class StringBooleanCellEditor : ComboBox
{
public StringBooleanCellEditor()
{
DropDownStyle = ComboBoxStyle.DropDownList;
ValueMember = "Key";
var values = new ArrayList
{
new ComboBoxItem("False", "Ложь"),
new ComboBoxItem("True", "Истина")
};
DataSource = values;
}
}
我重命名了class,它的名字更适合代码。