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