-1

I have a combobox that displays items from an ObservableCollection (which contains several directory links. I then have several treeviews and datatables, all of which needs to reference the directories based on the combobox selection. I know I've seen a similar question in the past just haven't been able to find it since!! Any direction would be appreciated!

For example the collection's contents are:

    public class ProjectThread
{
    public String projectName { get; set; }
    public String Directory1 { get; set; }
    public String Directory2 { get; set; }
    public String Directory3 { get; set; }
}

Now I'm trying to figure out how to retrieve multiple value paths from a single combobox selection:

            <ComboBox x:Name="comboBox" 
              HorizontalAlignment="Left" 
              ItemsSource="{Binding Items}"
              DisplayMemberPath="projectName" 
              SelectedValuePath="Directory1"/> (**How to reference multiple?)

Here's how my treeviews are being initialized:

        public MainWindow()
    {
        InitializeComponent();
        this.ListDirectory(treeView1, **unsure how to reference directory from combobox**"); 
        this.ListDirectory(treeView2, **unsure how to reference directory from combobox**"); 
    }

And here's my treeview backend:

        private void ListDirectory(TreeView treeView, string path)
    {
        treeView.Items.Clear();
        var rootDirectoryInfo = new DirectoryInfo(path);
        treeView.Items.Add(CreateDirectoryNode(rootDirectoryInfo));
    }

    private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        var directoryNode = new TreeViewItem { Header = directoryInfo.Name };
        foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Items.Add(CreateDirectoryNode(directory));

        foreach (var file in directoryInfo.GetFiles())
            directoryNode.Items.Add(new TreeViewItem { Header = file.Name });

        return directoryNode;

    }

As far as the "Duplicate thread" comment, I think that's not the case. The link refers to datagrid selections. My question is regarding combobox selection whose items have multiple properties. My question was how to pull multiple properties from the same selection in XAML.

ctalley5
  • 77
  • 3
  • 13
  • 1
    You best bet is to use some MVVM-like solution, where you have a viewmodel with `SelectedProject` bound to the ComboBox and some `Directory1, Directory2 ...` properties that are calculated according to the `SelectedProject`, like in http://stackoverflow.com/questions/33814890/wpf-mvvm-display-view-for-datagrids-selecteditem. Or if you don't want to use/learn MVVM (though I'd rather recommend to do it) you can take a look at the [SelectionChanged](http://stackoverflow.com/questions/2961118/wpf-combobox-selectionchanged-event-has-old-value-not-new) event on the combobox. – Eugene Podskal Jul 02 '16 at 16:14
  • Thanks for the suggestion, Eugene. I'm early in programming education and slowly getting toward MVVM. I'll most likely refactor my code (and this item) to MVVM in future, but for now I was hoping for a XAML solution which I happened across and posted in answer below. Thanks for your input!!! – ctalley5 Jul 10 '16 at 03:50

1 Answers1

0

I found the answer to my question. I had a hard time finding this solution, so here it is in case anyone happens upon the same trouble!

Instead of using SelectedValuePath... I was able to bind to multiple properties of my selection by doing this:

Treeview #1

ItemsSource="{Binding ElementName =comboBox, Path=SelectedItem.Directory1, UpdateSourceTrigger=PropertyChanged}"

Treeview #2

ItemsSource="{Binding ElementName =comboBox, Path=SelectedItem.Directory2, UpdateSourceTrigger=PropertyChanged}"

Treeview #3

ItemsSource="{Binding ElementName =comboBox, Path=SelectedItem.Directory3, UpdateSourceTrigger=PropertyChanged}"
ctalley5
  • 77
  • 3
  • 13