By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Unable to cast object of type TreeViewItem to custom type

P: n/a
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?

Nov 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a

<br************@gmail.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
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.
Nov 14 '07 #2

P: n/a
On Nov 14, 9:52 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
<bryan.kardi...@gmail.comwrote in message

news:11*********************@19g2000hsx.googlegrou ps.com...
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.
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

Nov 14 '07 #3

P: n/a
On Nov 14, 3:09 pm, bryan.kardi...@gmail.com wrote:
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
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

Nov 14 '07 #4

P: n/a
On Nov 14, 10:12 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 14, 3:09 pm, bryan.kardi...@gmail.com wrote:
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

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);

Nov 14 '07 #5

P: n/a
On Nov 14, 3:21 pm, bryan.kardi...@gmail.com wrote:

<snip>
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

Nov 14 '07 #6

P: n/a
On Nov 14, 10:28 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 14, 3:21 pm, bryan.kardi...@gmail.com wrote:

<snip>
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
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

Nov 14 '07 #7

P: n/a
br************@gmail.com 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.

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.
Nov 14 '07 #8

P: n/a
Chris Shepherd wrote:
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.
Nov 14 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.