简体   繁体   English

清单项目未清除

[英]List Items are not clearing

I have a form in which I am displaying another form: 我有一个正在显示另一种表单的表单:

if (headerText == "")
{
    MhrtTemplateColumn objMhrtTemplateColumn = 
            new MhrtTemplateColumn("", lstUnusedChannelTags);
    objMhrtTemplateColumn.ShowDialog();
}
else
{
    MhrtTemplateColumn objMhrtTemplateColumn = 
            new MhrtTemplateColumn(ChannelDesc, lstUnusedChannelTags, CurrentTag);
    objMhrtTemplateColumn.ShowDialog();
}

These are the overloaded constructors: 这些是重载的构造函数:

public MhrtTemplateColumn(string channelDescription, List<string> channelTags)
{
    InitializeComponent();
    this.ChannelDescription = channelDescription;
    this.ChannelTags = new List<string>();
    this.ChannelTags.Clear();            
    this.ChannelTags = channelTags;
}
public MhrtTemplateColumn(string channelDescription, List<string> channelTags, string CurrentChannelTag)
{
    InitializeComponent();
    this.ChannelDescription = channelDescription;
    this.ChannelTags = new List<string>();
    this.ChannelTags.Clear();
    this.ChannelTags = channelTags;
    this.CurrentChannelTag = CurrentChannelTag;
}

This is the Window_Loaded event for MhrtTemplateColumn: 这是MhrtTemplateColumn的Window_Loaded事件:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    txtChannelDescription.Text = ChannelDescription;
    if (CurrentChannelTag != null && CurrentChannelTag != "")
    {
        if(ChannelTags.Contains(CurrentChannelTag) == false)
            ChannelTags.Add(CurrentChannelTag);
        cmbChannelTag.ItemsSource = null;
        cmbChannelTag.ItemsSource = ChannelTags;
        cmbChannelTag.SelectedValue = CurrentChannelTag;
    }
    else
    {
        cmbChannelTag.ItemsSource = null;
        cmbChannelTag.ItemsSource = ChannelTags;
        cmbChannelTag.SelectedIndex = 0;
    }           
}

My problem is when the else block is executed, a new item gets added to the list. 我的问题是,当执行else块时,新项目将添加到列表中。 After closing the form and displaying it again with code in if block the list still has the new item added previously. 关闭表单并使用if块中的代码再次显示该表单后,列表仍然具有先前添加的新项。 Why? 为什么?

this.ChannelTags = new List<string>();
this.ChannelTags.Clear();            
this.ChannelTags = channelTags;

I think with this code you are trying to make a copy of channelTags ? 我认为使用此代码您正在尝试复制channelTags吗? If so, this is how to do that: 如果是这样,这是这样做的方法:

this.ChannelTags = channelTags.ToList();

Your code just creates an empty list, clears it, then throws it away and assigns channelTags to the field instead. 您的代码只是创建一个空列表,将其清除,然后将其丢弃,然后将channelTags分配给该字段。

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

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