简体   繁体   English

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

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

I create a customized ListView that inherited from standard ListView in .NET to show an enum values as its Items ( WinForms project): 我创建了一个自定义的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 is my enum: ScrapGrouop是我的枚举:

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

when I put the ScrapGroupsListView to my form, everything is OK and the control has no Items: 当我把ScrapGroupsListView放到我的表单时,一切正常,控件没有Items:

在此输入图像描述

But each time I build my project, the ScrapGroup values add to ScrapGroupsListView.Items (design-time): 但是每次构建项目时, ScrapGroup值都会添加到ScrapGroupsListView.Items (设计时):

after 1st build: 第一次构建后:

在此输入图像描述

after 2nd build: 第二次构建后:

在此输入图像描述

and so on. 等等。

Where is the problem? 问题出在哪儿?

Instead of constructor try: 而不是构造函数尝试:

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,
            });
        }
    }

Every time you open the form in the VS designer it creates this code: 每次在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});

This is why your items appear two times. 这就是您的商品出现两次的原因。 First set from the constructor, second set from InitializeComponent() . 首先从构造函数设置,第二个从InitializeComponent()设置。

You can (and should) remove the code from InitializeComponent() but opening the form in the designer will screw it up again. 您可以(并且应该)从InitializeComponent() )中删除代码,但在设计器中打开表单会再次将其搞砸。

To prevent from this you can move the filling code to a separate method and invoke it from the form. 为了防止这种情况,您可以将填充代码移动到单独的方法并从表单中调用它。

In your control: 在你的控制中:

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,
        } );
    }
}

In your form: 在你的形式:

public MainForm()
{
    InitializeComponent();

    scrapGroupsListView1.Fill();
}

As a drawback at this solution you don't see your items in the designer. 作为此解决方案的缺点,您在设计器中看不到您的项目。

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

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