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!