TreeViewItem UserControl

B

Bill McCormick

I have a TreeView that I want to populate with pre-built items through code.

Running the code (see Ref Code below) throws a XamlParseException:

Cannot create instance of 'Window1' defined in assembly
'WpfApplication1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. Exception has been thrown by the target of an
invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.

This sort of works, except the top level item is missing:
this.treeView.ItemsSource = pt.tviGroup.Items;

This does not work:
this.treeView.ItemsSource =
(System.Collections.IEnumerable) pt.tviGroup;

What is the correct way to do this?


Thanks,

Bill

*** Ref code below ***

Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TreeView Name="treeView">
</TreeView>
</Grid>
</Window>

MyGroups.xaml:
<UserControl x:Class="WpfApplication1.MyGroup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TreeViewItem Name="tviGroup" Header="Group" >
<TreeViewItem Name="tviItem1" Header="Item1" />
<TreeViewItem Name="tviItem2" Header="Item2">
<TreeViewItem
Name="tviItem2SubItem1" Header="Item2SubItem1" />
</TreeViewItem>
<TreeViewItem Name="tviItem3" Header="Item3" />
</TreeViewItem>
</UserControl>

Window1.xaml.cs
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
MyGroup myGroup = new MyGroup();
this.treeView.Items.Add(myGroup.tviGroup);
}
}
}
 
B

Bill McCormick

Bill said:
I have a TreeView that I want to populate with pre-built items through
code.

Running the code (see Ref Code below) throws a XamlParseException:

Cannot create instance of 'Window1' defined in assembly
'WpfApplication1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. Exception has been thrown by the target of an
invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.

This sort of works, except the top level item is missing:
this.treeView.ItemsSource = pt.tviGroup.Items;

This does not work:
this.treeView.ItemsSource =
(System.Collections.IEnumerable) pt.tviGroup;

What is the correct way to do this?


Thanks,

Bill

*** Ref code below ***

Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TreeView Name="treeView">
</TreeView>
</Grid>
</Window>

MyGroups.xaml:
<UserControl x:Class="WpfApplication1.MyGroup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TreeViewItem Name="tviGroup" Header="Group" >
<TreeViewItem Name="tviItem1" Header="Item1" />
<TreeViewItem Name="tviItem2" Header="Item2">
<TreeViewItem
Name="tviItem2SubItem1" Header="Item2SubItem1" />
</TreeViewItem>
<TreeViewItem Name="tviItem3" Header="Item3" />
</TreeViewItem>
</UserControl>

Window1.xaml.cs
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
MyGroup myGroup = new MyGroup();
this.treeView.Items.Add(myGroup.tviGroup);
}
}
}

I made the following changes to make this work but I'm not sure what
implications of this solution may be.

1. I put the tviGroup inside a <Grid>
2. this.treeView.ItemsSource = pt.grid.Children;

This seems to work OK for now but again, I'm not sure if there is a more
*correct* solution.

Any ideas out there in WPF land?



this.treeView.ItemsSource = pt.grid.Children;
 
Z

Zhi-Xin Ye [MSFT]

Hello Bill,

Thank you for using Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my
pleasure to work with you on this issue.

You can create a List<TreeViewItem> object to hold the items, and assign it
to the TreeView as item source, for example:

MyGroups myGroup = new MyGroups();
List<TreeViewItem> items = new List<TreeViewItem>();
items.Add(myGroup.tviGroup);
this.treeView.ItemsSource = items;

If you have any questions or concerns, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bill McCormick

Zhi-Xin Ye said:
Hello Bill,

Thank you for using Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my
pleasure to work with you on this issue.

You can create a List<TreeViewItem> object to hold the items, and assign it
to the TreeView as item source, for example:

MyGroups myGroup = new MyGroups();
List<TreeViewItem> items = new List<TreeViewItem>();
items.Add(myGroup.tviGroup);
this.treeView.ItemsSource = items;

If you have any questions or concerns, please feel free to let me know.
Thanks Zhi-Xin.

That's what I'm looking for.

Can you please comment on the solution that I've come up with here and
whether or not there is a more direct solution; some language feature
that lends itself to exactly this sort of thing?


Thanks,


Bill
 
Z

Zhi-Xin Ye [MSFT]

Hi Bill,

Your original solution:

public partial class Window1 : Window {
public Window1() {
InitializeComponent();
MyGroup myGroup = new MyGroup();
this.treeView.Items.Add(myGroup.tviGroup);
}
}

did not work because, the TreeViewItem in the UserControl already has a
logical parent, i.e. the "MyGroups" control, however, the
TreeView.Items.Add() method try to attach the TreeViewItem to the new
parent(the TreeView control), but the system requires the TreeViewItem to
be detached from the old parent before it is attached to the new one, which
results in the failure of creating the window instance, so you see the
error:
"
Cannot create instance of 'Window1' defined in assembly
'WpfApplication1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. Exception has been thrown by the target of an
invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.
"

However, if you put the code in the Window.Loaded event handler, you will
be able to see the exact error message,

void Window1_Loaded(object sender, RoutedEventArgs e)
{
MyGroup myGroup = new MyGroup();
this.treeView.Items.Add(myGroup.tviGroup);
}

Run the code above, you'll get this error message:
"Element already has a logical parent. It must be detached from the old
parent before it is attached to a new one."

And because the ItemsSource property is of IEnumerable type, so we should
assign an IEnumerable-based object to this property. The tviGroup in your
UserControl is of TreeViewItem type, it cannot be assign to
TreeView.ItemSource directly. That why I put it in a collection
(List<TreeViewItem> ).

However, why not put the TreeView and TreeViewItems together in the
UserControl? Or you can consider saving the tree view item information in
an XML file.

If you have any questions or concerns, please feel free to let me know.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel

free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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