Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same issue.</p> <p>The easiest way to implement the behavior of a treeview in a combobox is to create a TextBox and stylize it to look like a combobox. Add an image next to it. The trick is to put the treeview in a popup control. Then, when the user clicks the textbox or the dropdown image you chose, the popup is displayed directly under the textbox.</p> <p>Then, when the treeview item is selected, close the popup and place the text of the selected now in the textbox.</p> <p>Here's an unstylized example:</p> <p>XAML:</p> <pre><code>&lt;Window x:Class="ComboBoxTreeView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" MouseEnter="Window_MouseEnter"&gt; &lt;Grid Margin="15"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="30" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBox Grid.Row="0" x:Name="header" Width="300" Height="30" PreviewMouseDown="header_PreviewMouseDown" HorizontalAlignment="Left" /&gt; &lt;Popup Grid.Row="1" x:Name="PopupTest" AllowsTransparency="True" IsOpen="False"&gt; &lt;TreeView x:Name="Tree1" Initialized="Tree1_Initialized" SelectedItemChanged="Tree1_SelectedItemChanged"&gt; &lt;TreeViewItem Header="Test1" x:Name="Tree1Item1"&gt; &lt;TreeViewItem Header="1test1" /&gt; &lt;TreeViewItem Header="2test2" /&gt; &lt;/TreeViewItem&gt; &lt;TreeViewItem Header="Test2" /&gt; &lt;/TreeView&gt; &lt;/Popup&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>And here is the Code behind:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ComboBoxTreeView { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_MouseEnter(object sender, MouseEventArgs e) { } private void Tree1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e) { var trv = sender as TreeView; var trvItem = trv.SelectedItem as TreeViewItem; if (trvItem.Items.Count != 0) return; header.Text = trvItem.Header.ToString(); PopupTest.IsOpen = false; } private void Tree1_Initialized(object sender, EventArgs e) { var trv = sender as TreeView; var trvItem = new TreeViewItem() { Header="Initialized item"}; var trvItemSel = trv.Items[1] as TreeViewItem; trvItemSel.Items.Add(trvItem); } private void header_PreviewMouseDown(object sender, MouseButtonEventArgs e) { PopupTest.Placement = System.Windows.Controls.Primitives.PlacementMode.RelativePoint; PopupTest.VerticalOffset = header.Height; PopupTest.StaysOpen = true; PopupTest.Height = Tree1.Height; PopupTest.Width = header.Width; PopupTest.IsOpen = true; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload