I try to copy a TreeViewItem with all of it's items from a TreeView. When I try to paste it in the same TreeView but in an other location, I stumble on an error. This is my sample of the relevant code:
Some usefull datatypes
TreeView tvFolders; // The TreeView object
TreeViewItem bufferFolder; // A buffer object to store the copied TreeViewItem in
The method to copy the selected TreeViewItem
privatevoid Copy(object sender, RoutedEventArgs e)
if (tvFolders.SelectedItem == null)
bufferFolder = tvFolders.SelectedItem as TreeViewItem;
When I try to execute the selectedFolder.Items.Add(bufferFolder); instruction in the Paste() method, I get an InvalidOperationException with this following message:
"Element already has a logical parent. It must be detached from the old parent before it is attached to a new one."
The only way I know to "detach" the TreeViewItem is to remove it in Copy() method. But doing that actually redesigns the Copy() method to my Cut() method. That is not what I want, but it makes the Paste() method to work
I have tried several ways to copy the TreeViewItem with: "deep copy", "shallow copy", a derivided class of TreeViewItem with Serializable attribute and ISerializable interface as well as using the ICloneable interface but I have not had any luck.
You are approaching the problem in the wrong way. Basically, what you need to do is to take a copy of the data that makes up the TreeViewItem that you are interested in, and add this into the relevant location instead. As you have found, each element can only have one parent (the Visual and Logical Trees are tree structures after all), and it takes some extreme hackery to accomplish the "reparenting". By doing things the MVVM way instead, you will make things a lot easier on yourself.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
i created a custompanel inherited by form.panel. but when trying to dock the custompanel into form at runtime , i set it's Dock property
to DockStyle.fill, but still the custompanel adds to the form with the small size(100,100). and the onresize event is fired.
when I run,it show me this error:
"informations doesn't exist in the current context",informations is my database's name.
I think that I miss to add connection from my project(Tools--->connect to database"),this doesn't show any thing to help me because i couldn't add my database.
Someone to help me is so grantful
you are getting error "information doesn't esist in the current context" because you've not created any location holder(variable) of information. as you mention "information" is your database Name then OriginalGriff pointed you a perfect answer you should put it inside as "Database =information"
for more details you can go there[^] to learn basic of creating connection
I have a huge set of text files each being 100 kb to 1 mb. My Application reads the files (only 1 at a time) and process the string in it.
My problem is the total size of files are are very large. Is there any way, we can store the files in some format which takes lesser disk space. eg. byte array, or something...
Currently, I have zipped the files and reading the zipped file from my application. Is there any better way to do so.
You should consider using an MSSQL Compact Database. This is a database that is stored in a single file.
A database will generally store very large amounts of small data extremely efficiently - as well as providing a very rapid way to query that data.
Certainly worth considering - although without more information about what the data is, it's tricky to know!
If a database isn't suitable, then you will get a size decrease and performance increase if you use a binary file format rather than a text file format.
Also note that it can be rather inefficient space-wise to have lots and lots of small files, so again finding some way to aggregate the data into a larger file that can be rapidly accessed may be good.
When disk space is your primary concern:
- I don't see how a database would be more compact than plain text files;
- compression is what you want, ZIP is very good at compressing text (however it takes lots of cycles to decompress).
If you find anything significantly better than ZIP I will be very interested!