[英]Change the customized ListView.Items after each build
我創建了一個自定義的ListView
,它繼承自.NET
標准ListView
,以顯示enum
值作為其Items
( WinForms
項目):
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.