【问题标题】:C# combobox in ObjectListView column when enter value输入值时ObjectListView列中的C#组合框
【发布时间】:2017-05-10 07:07:14
【问题描述】:

我有来自 ObjectListView 库的带有 TreeListView 的 MainForm。

我想用不同的Windows.Forms.Controls 组件在ValueColumn(第二列)中输入值。

TreeView(名称为 jsonTreeView)正常显示所有值及其类型。它基于我自己的课程:

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。我想为不同类型的值创建不同的输入控件(它作为字符串保存在我的类中,但是当转换为 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();
    }
    //...
}

文档说:

一旦单元格编辑器被创建,它就会通过控件的 Value 属性被赋予单元格的值(如果它有一个并且它是可写的)。如果它没有可写的 Value 属性,则其 Text 属性将设置为单元格值的文本表示形式。

当用户完成对单元格中的值的编辑后,新值将被写回到模型对象中(如果可能的话)。为了获取修改后的值,默认处理会再次尝试使用 Value 属性。如果它不起作用,将使用 Text 属性。

但是当我尝试使用组合框设置任何值时(此控件 HAS Text 属性)返回值是 null。 我不仅尝试在组合框中添加字符串,还尝试在自定义和标准类中添加 - 没有任何反应。

我该怎么做?

【问题讨论】:

    标签: c# combobox objectlistview treelistview


    【解决方案1】:

    我找到了一些解决方案(不太好,但解决了问题)。

    ObjectListView 库的源代码中,我找到了BooleanCellEditor 类。它继承自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;
        }
    }
    

    我重命名了更适合代码的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多