2016-07-02 126 views
-1

我有一个组合框,显示来自ObservableCollection(包含多个目录链接)的项目 然后我有几个树视图和数据表,所有这些都需要根据组合框选择引用目录。知道我已经看到在过去类似的问题只是一直没能找到它,因为!!任何方向将不胜感激从单个组合框选择中选择多个值

例如收集的内容是:

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

现在的我m试图找出如何从单个组合框选择检索多个值路径离子:

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

下面是我的树视图正在初始化:

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

这是我的TreeView的后端:

 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; 

    } 

至于 “复制线程” 的评论,我认为这是不是这样。该链接指的是数据网格选择。我的问题是关于其项目具有多个属性的组合框选择。我的问题是如何从XAML中的相同选择中提取多个属性。

+1

最好的办法是使用一些类似MVVM的解决方案,在这里你有一个视图模型,它具有绑定到ComboBox的'SelectedProject'和一些'Directory1,Directory2 ...'属性,这些属性是根据'SelectedProject'计算出来的,就像http ://stackoverflow.com/questions/33814890/wpf-mvvm-display-view-for-datagrids-selecteditem。或者如果你不想使用/学习MVVM(虽然我宁愿推荐这样做),你可以看看[SelectionChanged](http://stackoverflow.com/questions/2961118/wpf-combobox-选择已更改事件具有旧值非新事件)组合框上的事件。 –

+0

感谢您的建议,尤金。我很早就开始编程教育,并慢慢向MVVM迈进。我很可能将我的代码(和这个项目)重构为未来的MVVM,但现在我希望有一个我发生的XAML解决方案,并发布在下面的答案中。感谢您的输入!!! – ctalley5

回答

0

我找到了我的问题的答案。我很难找到这个解决方案,所以在这里它可以防止任何人遇到同样的问题!

而不是使用SelectedValuePath的...我能够这样做是为了绑定到我的选择的多个属性:

树形#1

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

树形#2

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

Treeview#3

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