0

I have a treeview template like this:

<HierarchicalDataTemplate DataType="{x:Type data:Category}" ItemsSource="{Binding Path=Products}">
    <TextBlock Text="{Binding Path=CategoryName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:Product}">
    <StackPanel>
        <StackPanel.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Add To Project" Click="MenuItem_OnClick"/>
            </ContextMenu>
        </StackPanel.ContextMenu>
        <TextBlock Text="{Binding Path=ModelName}" />
    </StackPanel>
</HierarchicalDataTemplate>

And I am trying to add the treeview item to the linkedlist:

LinkedList<Product> dll = new LinkedList<Product>();
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
     var itemToAdd = this.tv_Project.SelectedItem as Product;
     Product previous = dll.ElementAt(dll.Count - 1);

     if(itemToAdd.CategoryID == 1)
     {
          dll.AddLast(ItemToAdd);
     }
     else if(itemToAdd.CategoryID == 2)
     {
          itemToAdd.ProductValue = previous.ProductValue + 1;
     }
     ...
}

Now when I run the above code, I found that if the previous(which I added to the linkedlist last time) and the itemToAdd(which I'm going to add to linkedlist this time) are the same, it changes the Property ProductValue of both previous and itemToAdd when this code execute:

itemToAdd.ProductValue = previous.ProductValue + 1;

So how should I fix this? Thanks in advance!

  • The LinkedList contains *references* to Product objects. So if two references refer to the same object, this object may be changed using any of these references. – mm8 Sep 15 '17 at 10:38
  • @mm8 Thanks for your reply. I believe so. I alse tried `Array`, it has the same problem. So could you please tell me what should I do if I want to achieve that? – user8595258 Sep 17 '17 at 07:05
  • I don't understand what your code is supposed to do and what you are trying to accomplish. And using an Array instead of a LinkedList makes no difference at all. You are still storing references. – mm8 Sep 18 '17 at 09:15
  • I want to change the invoke value but not the reference. The value in the linkedlist should be changed dynamic but not the `SelectedItem`. In this case: when I execute `itemToAdd.ProductValue = previous.ProductValue + 1;' the `previous.ProductValue` shouldn't be changed while the itemToAdd.ProductValue` shall be changed – user8595258 Sep 18 '17 at 09:56
  • The value in the list and the SelectedItem are the same. – mm8 Sep 18 '17 at 09:58

1 Answers1

0

The LinkedList<T> contains references to Product objects. So if two references refer to the same object, this object may be changed using any of these references.

You may want to try to add copies of products to the LinkedList<T>:

/* create a new Product object here and set all of its properties: */
ddl.AddLast(new Product() { Name = ItemsToAdd.Name });

You may also want to read up on how reference types work in .NET.

And on the differences between reference and value types:

What is the difference between a reference type and value type in c#?

Depending on your requirements, you may want to define Product as a value type (struct).

mm8
  • 163,881
  • 10
  • 57
  • 88