Binding to a single element inside a CompositeCollection

前端 未结 4 808
清酒与你
清酒与你 2020-12-11 05:58

I am trying to produce a list of servers for browsing on a network such that it produces a tree view which looks like this:

-Local Server
 - Endpoint 1
 - En         


        
4条回答
  •  無奈伤痛
    2020-12-11 06:31

    Well, this is the closest I can come to your requirements. All the functionality is not contained within one TreeView, nor is it bound to a compositecollection, but that can remain a secret between you and me;)

    
    
        
            
            
                
        
        
        
            
                
                
            
        
    
    

    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace CompositeCollectionSpike
    {
        public partial class Window1 : Window
        {
            private ViewModel viewModel;
            public Window1()
            {
                InitializeComponent();
                viewModel = new ViewModel
                                {
                                    LocalServer =new ServerCollection{new Server()},
                                    RemoteServers =
                                        new ServerCollection("Remote Servers") {new Server(),
                                            new Server(), new Server()},
                                };
                DataContext = viewModel;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                viewModel.LaunchAddRemoteServerDialog();
            }
        }
    
        public class ViewModel:DependencyObject
        {
            public ServerCollection LocalServer { get; set; }
            public ServerCollection RemoteServers { get; set; }
    
            public void LaunchAddRemoteServerDialog()
            {}
        }
    
        public class ServerCollection:ObservableCollection
        {
            public ServerCollection(){}
    
            public ServerCollection(string description)
            {
                Description = description;
            }
            public string Description { get; set; }
        }
    
        public class Server
        {
            public static int EndpointCounter;
            public static int ServerCounter;
            public Server()
            {
                Name = "Server"+ ++ServerCounter;
                EndPoints=new ObservableCollection();
                for (int i = 0; i < 2; i++)
                {
                    EndPoints.Add("Endpoint"+ ++EndpointCounter);
                }
            }
            public string Name { get; set; }
            public ObservableCollection EndPoints { get; set; }
        }
    }
    

提交回复
热议问题