繁体   English   中英

每次构建后更改自定义ListView.Items

[英]Change the customized ListView.Items after each build

我创建了一个自定义的ListView ,它继承自.NET标准ListView ,以显示enum值作为其ItemsWinForms项目):

public class ScrapGroupsListView : ListView
{
    public event EventHandler SelectedColorChanged;

    private ScrapGroup _selectedGroup = ScrapGroup.None;

    [DefaultValue(ScrapGroup.None)]
    public ScrapGroup SelectedScrapGroup
    {
        get { return _selectedGroup; }
        set
        {
            if (_selectedGroup != value)
            {
                _selectedGroup = value;
                foreach (ListViewItem item in this.Items)
                {
                    if (item != null)
                    {
                        if (item.Tag != null)
                        {
                            var itemColor = (ScrapGroup)item.Tag;
                            if (itemColor == ScrapGroup.None) 
                                item.Checked = value == ScrapGroup.None;
                            else
                                item.Checked = value.HasFlag(itemColor);
                        }
                    }
                }

                if (SelectedColorChanged != null) 
                   SelectedColorChanged.Invoke(this, EventArgs.Empty);
            }
        }
    }

    public ScrapGroupsListView()
    {
        this.Items.Clear();
        this.CheckBoxes = true;
        this.HeaderStyle = ColumnHeaderStyle.None;
        this.View = View.List;

        foreach (var value in Enum.GetValues(typeof(ScrapGroup)).Cast<ScrapGroup>())
        {
            this.Items.Add(new ListViewItem()
            {
                Name = value.ToString(),
                Text = value.ToString(),
                Tag = value,
            });
        }
    }

    protected override void OnItemChecked(ItemCheckedEventArgs e)
    {
        base.OnItemChecked(e);

        var checkedScrapGroup = (ScrapGroup)e.Item.Tag;

        if (e.Item.Checked)
            if (checkedScrapGroup == ScrapGroup.None)
                SelectedScrapGroup = ScrapGroup.None;
            else
                SelectedScrapGroup |= checkedScrapGroup;
        else
            SelectedScrapGroup &= ~checkedScrapGroup;
    }
}

ScrapGrouop是我的枚举:

[Flags]
public enum ScrapGroup
{
    None=0,
    M=1,
    E=2,
    N=4,
    H=8
}

当我把ScrapGroupsListView放到我的表单时,一切正常,控件没有Items:

在此输入图像描述

但是每次构建项目时, ScrapGroup值都会添加到ScrapGroupsListView.Items (设计时):

第一次构建后:

在此输入图像描述

第二次构建后:

在此输入图像描述

等等。

问题出在哪儿?

而不是构造函数尝试:

protected override void OnCreateControl()
    {
        base.OnCreateControl();

        this.Items.Clear();
        this.CheckBoxes = true;
        this.HeaderStyle = ColumnHeaderStyle.None;
        this.View = View.List;

        foreach (var value in Enum.GetValues(typeof(ScrapGroup)).Cast<ScrapGroup>())
        {
            this.Items.Add(new ListViewItem()
            {
                Name = value.ToString(),
                Text = value.ToString(),
                Tag = value,
            });
        }
    }

每次在VS设计器中打开表单时,它都会创建以下代码:

private void InitializeComponent()
{
    System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("None");
    System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("M");
    System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("E");
    System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("N");
    System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("H");
    this.scrapGroupsListView1 = new ScrapGroupsListView();
    // 
    // scrapGroupsListView1
    // 
    this.scrapGroupsListView1.CheckBoxes = true;
    this.scrapGroupsListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
    listViewItem1.StateImageIndex = 0;
    listViewItem1.Tag = ScrapGroup.None;
    listViewItem2.StateImageIndex = 0;
    listViewItem2.Tag = ScrapGroup.M;
    listViewItem3.StateImageIndex = 0;
    listViewItem3.Tag = ScrapGroup.E;
    listViewItem4.StateImageIndex = 0;
    listViewItem4.Tag = ScrapGroup.N;
    listViewItem5.StateImageIndex = 0;
    listViewItem5.Tag = ScrapGroup.H;
    this.scrapGroupsListView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
    listViewItem1,
    listViewItem2,
    listViewItem3,
    listViewItem4,
    listViewItem5});

这就是您的商品出现两次的原因。 首先从构造函数设置,第二个从InitializeComponent()设置。

您可以(并且应该)从InitializeComponent() )中删除代码,但在设计器中打开表单会再次将其搞砸。

为了防止这种情况,您可以将填充代码移动到单独的方法并从表单中调用它。

在你的控制中:

public ScrapGroupsListView()
{
    this.CheckBoxes = true;
    this.HeaderStyle = ColumnHeaderStyle.None;
    this.View = View.List;
}

public void Fill()
{
    this.Items.Clear();

    foreach( var value in Enum.GetValues( typeof( ScrapGroup ) ).Cast<ScrapGroup>() )
    {
        this.Items.Add( new ListViewItem()
        {
            Name = value.ToString(),
            Text = value.ToString(),
            Tag = value,
        } );
    }
}

在你的形式:

public MainForm()
{
    InitializeComponent();

    scrapGroupsListView1.Fill();
}

作为此解决方案的缺点,您在设计器中看不到您的项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM