Click to See Complete Forum and Search --> : Combobox in TreeViewItem


levik
March 25th, 2009, 11:38 AM
Hi all!

I'm newbie in WPF, so sorry about stupid question.
It is possible to show combobox side by side with selected TreeViewItem?
I need something like shown in the 1.jpg picture.
I tried to do thus:
<TreeView Name="treeView1">
<TreeViewItem Header="aaa">
<ComboBox Height="19">
<ComboBoxItem IsSelected="True">111</ComboBoxItem>
<ComboBoxItem>222</ComboBoxItem>
<ComboBoxItem>333</ComboBoxItem>
</ComboBox>
<TreeViewItem Header="aaa1">
</TreeViewItem>
<TreeViewItem Header="aaa2">
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="bbb">
<TreeViewItem Header="bbb1" />
<TreeViewItem Header="bbb2" />
</TreeViewItem>
<TreeViewItem Header="ccc" />
</TreeView>

and the result you can see in the 2.jpg picture.

Meantime I need to know, how to do this visually. Later I need to do something with SelectedItemChanged event.

Thanks in advance!

P.S. sorry about my english

gstercken
April 20th, 2009, 08:21 AM
It is possible to show combobox side by side with selected TreeViewItem?
Definitely! But in order to achieve this, you need make the combobox part of the TreeViewItem's header, instead of adding it to the child items.

In order to display the label string and the combobox side-by-side, you need to wrap both in an appropriate panel (for example, a stack panel).

Here's your updated XAML:
<TreeView Name="treeView1">
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Label>aaa</Label>
<ComboBox Height="19">
<ComboBoxItem IsSelected="True">111</ComboBoxItem>
<ComboBoxItem>222</ComboBoxItem>
<ComboBoxItem>333</ComboBoxItem>
</ComboBox>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem Header="aaa1">
</TreeViewItem>
<TreeViewItem Header="aaa2">
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="bbb">
<TreeViewItem Header="bbb1" />
<TreeViewItem Header="bbb2" />
</TreeViewItem>
<TreeViewItem Header="ccc" />
</TreeView>

A real-world solution would rather use a ControlTemplate for applying this to all items at once - this is just to show how you can generally replace the item's header by any UIElement.