cheesetarmac

Hi

I have a class structure conceptually similar to the following that I'd like to be able to display in a treeView using an ObjectDataProvider and HierarchicalDataTemplates

public class MyContainer
{

private ObservableCollectionWithPropertyChanged<Person> m_Contributors = new ObservableCollectionWithPropertyChanged<Person>();

public ObservableCollectionWithPropertyChanged<Person> Contributors
{
get { return m_Contributors; }
set { m_Contributors = value; }
}

private ObservableCollectionWithPropertyChanged<KeyQuestion> m_KeyQuestions = new ObservableCollectionWithPropertyChanged<KeyQuestion>();

public ObservableCollectionWithPropertyChanged<KeyQuestion> KeyQuestions
{
get { return m_KeyQuestions; }
set { m_KeyQuestions = value; }
}
}

The problem I have is that HierarchicalDataTemplates only allow for one ItemsSource so I can bind to either Contributors or KeyQuestions but not both. I'd really like to be able to see something like this

+ KeyQuestions
- KeyQuestion 1
- KeyQuestions2
...
+ Contributors
- Person1
- Person2
- Person3

Is there a way to achieve this

regards

Steve


Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

cheesetarmac

Just to illustrate things a little bit more, what I'm conceptually after is something similar to what you can see in the Visual Tree of Blend.

For example, say I Added a DockPanel to my window and then a couple of ToolBars, a menu, status bar etc. When viewed in Blend, Expanding the DockPanel would show the StatusBar and Menu and leaf nodes and the two Toolbars as branches. Simalarly other controls that are containers would also be branches.

I guess one question is, is the visual tree in Blend created using the TreeView control and HeirarchicalDataTemplates




Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

lee d

you can try creating a property of type dictionary and add 2 your collections and bind the dictionary to treeview's itemssource.

<HierarchicalDataTemplate x:Key="dt1"

ItemsSource = "{Binding Path=Value}">

<TextBlock Text="{Binding Path=Key}"/>

</HierarchicalDataTemplate>

set this template as itemtemplate of the treeview. add 2 more datatemplates with datatypes of your objects.

That should work






Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

cheesetarmac

Hi

Thanks for the suggestion, however I don't think this is going to work for me, because there are additional "Container" types at subsequent levels for my data structure. However that said I'm keen to give this a go but I don't quite get the example you've shown I'm using an ObjectDataProvider for my tree eg
<TreeView VerticalAlignment="Stretch" Margin="0,0,0,0" Width="586" x:Name="TreeView" ItemsSource="{Binding Source={StaticResource ODPData}}"/>

So, my template definitions are like this

<HierarchicalDataTemplate DataType = "{x:Type MyAssembly:MyContainer}"
ItemsSource = "{Binding Path=KeyQuestions}">
<StackPanel HorizontalAlignment="Stretch" Background="Khaki" Opacity="0.8">
<TextBlock Text="{Binding Path=Affiliation, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType = "{x:Type MyAssembly:KeyQuestion}"
ItemsSource = "{Binding Path=AnalysisOptions}">
<StackPanel HorizontalAlignment="Stretch" Background="Khaki" Opacity="0.8">
<TextBlock Text="{Binding Path=Question, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</HierarchicalDataTemplate>




Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

lee d

try something like this in container class

private Dictionary<string,object> _dict;

public Dictionary<string,object> Dict

{

get { return _dict; }

set { _dict = value; }

}

in the constructor you could do

_dict = new Dictionary<string,object>();

_dict.Add("Questions",KeyQuestions);

_dict.Add("Contributors", Contributors);

and for datatemplates have 2 datatemplates like you have

Itemssource will point to the dictionary property.for treeview set the itemtemplate to something like this(this is going to be the dictionary item)

<HierarchicalDataTemplate x:Key="dta"

ItemsSource = "{Binding Path=Value}">

<TextBlock Text="{Binding Path=Key}"/>

</HierarchicalDataTemplate>






Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

Rob Relyea

cheesetarmac-

how did that work for you

unless we hear differently, we'll mark that as the answer...(but we'd much prefer you to say definitively...)

Thanks, Rob Relyea

Program Manager, WPF Team

http://rrelyea.spaces.live.com





Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

busteesm

I have almost the same problem but did not quite understand the dictonairy solution mentioned. I am trying to get a treeview setup, that will show a structure where, a MenuCategory can contain either an MenuItem, or a MenuGroup, that contains other MenuItems.

quasi-code:

class MenuCategory{

MenuItem[] MenuItems;

MenuGroup[] MenuGroups;

}

class MenuGroup{

MenuItem[] MenuItems

}

class MenuItem

{

//Item variables

I have a hierarchical datatemplate up that will read the categories and then if the item source is set to Menuitem it will show those, but I need it to go to the "MenuItem" or "MenuGroup" depending on which is there.

<HierarchicalDataTemplate DataType= "{x:Type evDeliv_Client:MenuCategory}"
ItemsSource="{Binding Path=MenuGroup}">
<Grid Background="{x:Null}" MinWidth="500" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<TextBlock Text="{Binding Mode=OneWay, Path=Name}" FontSize ="16pt" Foreground="#FFB9C8F4" TextAlignment="Center" Grid.Column="1" />

</Grid>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType = "{x:Type evDeliv_Client:MenuGroup}"
ItemsSource="{Binding Path=MentItem}">
<DockPanel Background="{x:Null}" MinWidth = "500" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" LastChildFill="True" >
<TextBlock Text="{Binding Mode=OneWay, Path=Name}" Foreground="#FFB9C8F4" Width = "250" DockPanel.Dock="Left" FontSize ="12pt" />
<TextBlock Text="{Binding Mode=OneWay, Path=Description}" Foreground="#FFB9C8F4" Width = "250" DockPanel.Dock="Left" FontSize ="12pt" />
</DockPanel>
</HierarchicalDataTemplate>


<HierarchicalDataTemplate DataType = "{x:Type evDeliv_Client:MenuItem}">


<DockPanel Background="{x:Null}" MinWidth = "500" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" LastChildFill="True" >
<TextBlock Text="{Binding Mode=OneWay, Path=Name}" Foreground="#FFB9C8F4" Width = "250" DockPanel.Dock="Left" FontSize ="12pt" />
<Grid Width = "200" DockPanel.Dock="Right">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<TextBlock Text="{Binding Mode=OneWay, Path=Price}" Foreground="#FFEC9A49" Grid.Column="2" FontSize ="12pt" />
</Grid>
</DockPanel>

</HierarchicalDataTemplate>
Would the dictionairy solution mentioned above, work for this And do you know how I can set this up





Re: Windows Presentation Foundation (WPF) HierarchicalDataTemplate and multiple ItemSources

cheesetarmac

Hi

I never actually managed to get the dictionary approach to work for me as I recall. I think it would work to one level but I couldn't get it to then work for the next level down.

regards

Steve