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

TreeViewItem UserControl

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.