Unable to cast object of type TreeViewItem to custom type

B

bryan.kardisco

I'm currently trying to extend the TreeViewItem class so I can attach
some additional attributes to the item.

I created a custom class

class myTreeNode : System.Windows.Controls.TreeViewItem
{
public bool updated;
public myTreeNode()
{

}

}
}


I then tried to do the following::
private void treeDoubleClick(object sender,
System.Windows.Input.MouseButtonEventArgs e)
{

System.Windows.Controls.TreeView currentTreeView =
(System.Windows.Controls.TreeView)sender;

myTreeNode myNode = (myTreeNode)((TreeView)sender).SelectedItem;
myTreeNode.updated = false;
....
}

But I get the error Unable to cast object of type TreeViewItem to type
myTreeNode


Any suggestions?
 
B

Ben Voigt [C++ MVP]

I'm currently trying to extend the TreeViewItem class so I can attach
some additional attributes to the item.

I created a custom class

class myTreeNode : System.Windows.Controls.TreeViewItem
{
public bool updated;
public myTreeNode()
{

}

}
}


I then tried to do the following::
private void treeDoubleClick(object sender,
System.Windows.Input.MouseButtonEventArgs e)
{

System.Windows.Controls.TreeView currentTreeView =
(System.Windows.Controls.TreeView)sender;

myTreeNode myNode = (myTreeNode)((TreeView)sender).SelectedItem;
myTreeNode.updated = false;
....
}

But I get the error Unable to cast object of type TreeViewItem to type
myTreeNode


Any suggestions?

Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.
 
B

bryan.kardisco

Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.

I don't think I'm entirely clear of what you mean...

I appologize for my ignorance on the subject matter and lack of
understanding of what you're suggesting I do
 
J

Jon Skeet [C# MVP]

I don't think I'm entirely clear of what you mean...

I appologize for my ignorance on the subject matter and lack of
understanding of what you're suggesting I do

When you populate the tree with nodes, how are you doing it? If you're
not adding instances of your own class to start with, you won't be
able to later "pretend" that the selected node is a different type.

Jon
 
B

bryan.kardisco

When you populate the tree with nodes, how are you doing it? If you're
not adding instances of your own class to start with, you won't be
able to later "pretend" that the selected node is a different type.

Jon

Initiallaly I am going

TreeView myTreeView = new TreeView();
TreeViewItem myItem = new TreeViewItem();
....
myTreeView.Items.Add(myItem);

However, later I want to create new treeViewItems with additional
attributes so I assumed I could make a class called MyTreeNode and
have it extend the TreeViewItem class and do something like

myTreeNode myNode = new myTreeNode();
myNode.attribute = "foobar";

myTreeView.Items.Add(myNode);
 
J

Jon Skeet [C# MVP]

On Nov 14, 3:21 pm, (e-mail address removed) wrote:

Initiallaly I am going

TreeView myTreeView = new TreeView();
TreeViewItem myItem = new TreeViewItem();
...
myTreeView.Items.Add(myItem);

However, later I want to create new treeViewItems with additional
attributes so I assumed I could make a class called MyTreeNode and
have it extend the TreeViewItem class and do something like

myTreeNode myNode = new myTreeNode();
myNode.attribute = "foobar";

myTreeView.Items.Add(myNode);

That's fine, but what do you expect to happen if someone clicks on one
of the "plain" TreeViewItems that you originally populated the tree
with? They won't be instances of myTreeNode, which is why the cast
fails.

Jon
 
B

bryan.kardisco

On Nov 14, 3:21 pm, (e-mail address removed) wrote:







That's fine, but what do you expect to happen if someone clicks on one
of the "plain" TreeViewItems that you originally populated the tree
with? They won't be instances of myTreeNode, which is why the cast
fails.

Jon

That clears up the situation, I guess I need to just basically revamp
the whole backend and make every TreeViewItem an myTreeNode and give
it a bit of a better naming scheme to keep in line with best
practices ... the whole bit.

Thank you
 
C

Chris Shepherd

That clears up the situation, I guess I need to just basically revamp
the whole backend and make every TreeViewItem an myTreeNode and give
it a bit of a better naming scheme to keep in line with best
practices ... the whole bit.


Not really. You could instead use either _is_ or _as_ to accomplish what
you want to do:

foreach (TreeNode node in treeView.Nodes)
{
if (tvi is myTreeNode)
{
// Do your cast and work
}
}

That is of course assuming you don't mind them being mixed.


Chris.
 
C

Chris Shepherd

Chris said:
foreach (TreeNode node in treeView.Nodes)
{
if (tvi is myTreeNode)
{
// Do your cast and work
}
}

Changing it up on the fly doesn't always work out. "tvi" in the above
should be "node".

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top