473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

TreeViewItem UserControl

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);
}
}
}
Nov 21 '08 #1
1 5378
Bill McCormick wrote:
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;
Nov 21 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Veeneman | last post by:
Is there a simple way to pass drag-and-drop events to a child control in a UserControl? Here's an example: I have created a UserControl which contains a treeview and some text boxes. I want to...
8
by: Raed Sawalha | last post by:
Hi, I have a strange problem with a usercontrol on a page. The usercontrol dispalyes three categories (From a database) when the user clicks a category they see all the products in a shop for...
2
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
10
by: Benton | last post by:
Hi there, I have a UserControl with a couple of textboxes and a couple of buttons ("Save" and "Cancel"). The Click event for this buttons is in the UserControl's codebehind of course, so here's...
8
by: bryan.kardisco | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.