Binding objects to a tree structure xaml

北战南征 提交于 2020-01-15 11:53:07

问题


I am working on a WPF tree control. My class has objects which are a reference to another object. I want to be able to bind my objects in this class to be able to be bound to the tree in the xaml.

This is what I have in my ViewModel and I want to bind each to a tree structure :

class PerformNewVehicleInquiryModel{
        public List<AddressDataType> AddressDataVMField { get; set; }
        public List<NetworkContolType> NetworkControlVMField { get; set; }
        public List<IdentificationType> VehicleIdentificationVMField { get; set; }
        public List<PerformNewVehicleInquiryRequestType> PerformNewVehicleInquiryVMRequestField { get; set; }
        }

//here is the method to add data  to each node

    public List<PerformNewVehicleInquiryModel> getAllPerformNewVehicleInquiry()
    {
        // add to addressDataField List
        addressDataField.Add(new AddressDataType("AA", "A1"));
        addressDataField.Add(new AddressDataType("AA", "A1"));

        // add to networkControlField List
        //List<NetworkContolType> networkContolTypeList = new List<NetworkContolType>();
        networkControlField.Add(new NetworkContolType(addressDataField, "22", "1305231128040001 1UNISC"));

        // create and add to identificationType List
        List<IdentificationType> identificationTypeList = new List<IdentificationType>();
        identificationTypeList.Add(new IdentificationType("IdentificationType1"));
        identificationTypeList.Add(new IdentificationType("IdentificationType1"));

        // create and add to PerformNewVehicleInquiryRequestType List
        PerformNewVehicleInquiryRequestType performNewVehicleInquiryRequestType = new PerformNewVehicleInquiryRequestType(networkControlField, identificationTypeList);
        // add to list
        //List<PerformNewVehicleInquiryRequestType> performNewVehicleInquiryRequestTypeList = new List<PerformNewVehicleInquiryRequestType>();
        performNewVehicleInquiryRequestField.Add(performNewVehicleInquiryRequestType);

        // create and add to PerformNewVehicleInquiryModel
        performNewVehicleInquiryModel.Add(new PerformNewVehicleInquiryModel(addressDataField, networkControlField, identificationTypeList, performNewVehicleInquiryRequestField));

        return performNewVehicleInquiryModel;

    }

      // Bind tree to this observable list
    public ObservableCollection<PerformNewVehicleInquiryViewModel> PerformNewVehicleInquiries
    {
        get { return performNewVehicleInquiry; }
        set
        {
            if (performNewVehicleInquiry == value)
                return;
            performNewVehicleInquiry = value;
            NotifyPropertyChanged("PerformNewVehicleInquiries");
        }
    }

    I am having hard time to bind this to a tree structure.  it wont display the objects populated in the method above.

// Here is what my XAML Looks like now

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding PerformNewVehicleInquiryVMRequestField}">
                    <TextBlock Foreground="Red" Text="{Binding NetworkContolType }" />

                    <!-- NetworkControlVMField template -->
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding NetworkControlVMField}">
                            <TextBlock Text="{Binding AddressData}" />

                            <!-- AddressDataVMField template -->
                            <HierarchicalDataTemplate.ItemTemplate>
                                <HierarchicalDataTemplate ItemsSource="{Binding AddressDataVMField}">
                                    <TextBlock Text="{Binding ApplicationID}" />

                                    <!-- AddressDataVMField template -->
                                    <HierarchicalDataTemplate.ItemTemplate>

                                        <DataTemplate>
                                            <TextBlock Text="{Binding MessageOriginatorID }" />

                                        </DataTemplate>
                                    </HierarchicalDataTemplate.ItemTemplate>

                                </HierarchicalDataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>

                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>

                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

        </TreeView>

来源:https://stackoverflow.com/questions/16821322/binding-objects-to-a-tree-structure-xaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!