How to implement Expander view with multi level(tree view) from code behind in windows phone 8?

Nambukarthy

I have done this by using static records in xaml and which works perfectly, here in this example i have added three levels of expander view, my entire xaml code is below

<toolkit:ExpanderView x:Name="India" Header="India" FontSize="40" >
                            <toolkit:ExpanderView.Items>
                                <toolkit:ExpanderView x:Name="Karnataka" Header="Karnataka" FontSize="40" >
                                    <toolkit:ExpanderView.Items>
                                        <toolkit:ExpanderView x:Name="Bangalore" Header="Bangalore" FontSize="40" >
                                            <toolkit:ExpanderView.Items>
                                                <TextBlock Text="RamamurthyNagar"></TextBlock>
                                                <TextBlock Text="VasanthNagar"></TextBlock>
                                                <TextBlock Text="CentralSilkBoard"></TextBlock>
                                            </toolkit:ExpanderView.Items>
                                        </toolkit:ExpanderView>
                                    </toolkit:ExpanderView.Items>
                                </toolkit:ExpanderView>

                                <toolkit:ExpanderView x:Name="TamilNadu" Header="TamilNadu" FontSize="40" >
                                    <toolkit:ExpanderView.Items>
                                        <toolkit:ExpanderView x:Name="Coimbatore" Header="Coimbatore" FontSize="40" >
                                            <toolkit:ExpanderView.Items>
                                                <toolkit:ExpanderView x:Name="GandhiPuram" Header="Gandhipuram" FontSize="40" Width="200">
                                                    <toolkit:ExpanderView.Items>
                                                        <toolkit:ExpanderView x:Name="Sidhhapudhur" Header="Sidhhapudhur" FontSize="40" >
                                                        </toolkit:ExpanderView>
                                                    </toolkit:ExpanderView.Items>
                                                </toolkit:ExpanderView>
                                                <toolkit:ExpanderView x:Name="SaibabaColony" Header="SaibabaColony" FontSize="40" Width="200" >
                                                </toolkit:ExpanderView>
                                            </toolkit:ExpanderView.Items>
                                        </toolkit:ExpanderView>
                                    </toolkit:ExpanderView.Items>
                                </toolkit:ExpanderView>
                            </toolkit:ExpanderView.Items>
                        </toolkit:ExpanderView>

                        <toolkit:ExpanderView x:Name="Canada" Header="Canada" FontSize="40" >
                            <toolkit:ExpanderView.Items>
                                <toolkit:ExpanderView x:Name="BritishColombia" Header="BritishColombia" FontSize="40" >
                                    <toolkit:ExpanderView.Items>
                                        <TextBlock Text="Vancouver"></TextBlock>
                                    </toolkit:ExpanderView.Items>
                                </toolkit:ExpanderView>

                                <toolkit:ExpanderView x:Name="Ontario" Header="Ontario" FontSize="40" >
                                    <toolkit:ExpanderView.Items>
                                        <TextBlock Text="Toronoto"></TextBlock>
                                    </toolkit:ExpanderView.Items>
                                </toolkit:ExpanderView>

                            </toolkit:ExpanderView.Items>
                        </toolkit:ExpanderView>

                        <toolkit:ExpanderView x:Name="UnitedStates" Header="UnitedStates" FontSize="40" />

This is the screeshot

i want to bind the same details from code behind. Thanks in advance:)

Nambukarthy

Here the solution for my question.

Xaml Code:

<!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ListBox Grid.Row="0" x:Name="listCountries">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:ExpanderView Header="{Binding}" IsExpanded="{Binding IsExpanded1, Mode=TwoWay}" HeaderTemplate="{StaticResource FirstLevelHeaderTemplate}">
                            <!--ItemsSource="{Binding states}" ItemTemplate="{StaticResource FirstLevelItemTemplate}"-->
                            <toolkit:ExpanderView.Items>
                                <Grid Height="Auto">
                                    <ListBox  x:Name="listStates" ItemsSource="{Binding states}">
                                        <ListBox.ItemContainerStyle>
                                            <Style TargetType="ListBoxItem">
                                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                                            </Style>
                                        </ListBox.ItemContainerStyle>
                                        <ListBox.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel/>
                                            </ItemsPanelTemplate>
                                        </ListBox.ItemsPanel>
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <toolkit:ExpanderView Header="{Binding}"                                    
                                             IsExpanded="{Binding IsExpanded2, Mode=TwoWay}"
                                             HeaderTemplate="{StaticResource SecondLevelHeaderTemplate}">
                                                    <!-- ItemsSource="{Binding cities}" ItemTemplate="{StaticResource SecondLevelItemTemplate}"-->
                                                    <toolkit:ExpanderView.Items>
                                                        <Grid Height="Auto">
                                                            <ListBox  x:Name="listCities" ItemsSource="{Binding cities}">
                                                                <ListBox.ItemContainerStyle>
                                                                    <Style TargetType="ListBoxItem">
                                                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                                                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                                                                    </Style>
                                                                </ListBox.ItemContainerStyle>
                                                                <ListBox.ItemsPanel>
                                                                    <ItemsPanelTemplate>
                                                                        <StackPanel/>
                                                                    </ItemsPanelTemplate>
                                                                </ListBox.ItemsPanel>
                                                                <ListBox.ItemTemplate>
                                                                    <DataTemplate>
                                                                        <!--<TextBlock Text="{Binding City}"></TextBlock>-->
                                                                        <toolkit:ExpanderView Header="{Binding}"                                    
                                             IsExpanded="{Binding IsExpanded3, Mode=TwoWay}"
                                             HeaderTemplate="{StaticResource ThirdLevelHeaderTemplate}">
                                                                            <!-- ItemsSource="{Binding cities}" ItemTemplate="{StaticResource SecondLevelItemTemplate}"-->
                                                                            <toolkit:ExpanderView.Items>
                                                                                <Grid Height="Auto">
                                                                                    <ListBox  x:Name="listAreas" ItemsSource="{Binding areas}">
                                                                                        <ListBox.ItemContainerStyle>
                                                                                            <Style TargetType="ListBoxItem">
                                                                                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                                                                                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                                                                                            </Style>
                                                                                        </ListBox.ItemContainerStyle>
                                                                                        <ListBox.ItemsPanel>
                                                                                            <ItemsPanelTemplate>
                                                                                                <StackPanel/>
                                                                                            </ItemsPanelTemplate>
                                                                                        </ListBox.ItemsPanel>
                                                                                        <ListBox.ItemTemplate>
                                                                                            <DataTemplate>
                                                                                                <TextBlock Text="{Binding Area}"></TextBlock>
                                                                                            </DataTemplate>
                                                                                        </ListBox.ItemTemplate>
                                                                                    </ListBox>
                                                                                </Grid>
                                                                            </toolkit:ExpanderView.Items>
                                                                        </toolkit:ExpanderView>
                                                                    </DataTemplate>
                                                                </ListBox.ItemTemplate>
                                                            </ListBox>
                                                        </Grid>
                                                    </toolkit:ExpanderView.Items>
                                                </toolkit:ExpanderView>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>
                                </Grid>
                            </toolkit:ExpanderView.Items>
                        </toolkit:ExpanderView>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

CS Code:

 public partial class SecondPage : PhoneApplicationPage
    {
        public SecondPage()
        {
            InitializeComponent();

            List<Countries> objCountries = new List<Countries>()
            {
                new Countries() { Country = "India",  states = new List<States>
                {
                    new States() { State = "Karnataka" , cities = new List<Cities>
                    {
                        new Cities(){ City = "Bangalore", areas = new List<Areas>
                        {
                             new Areas(){Area="RamamurthyNagar"},
                             new Areas(){Area="VasanthNagar"},
                             new Areas(){Area="RamamurthyNagar"}}                        
                        },

                    }},
                    new States() { State = "TamilNadu", cities = new List<Cities>
                    {
                        new Cities(){ City = "Coimbatore", areas=null}}                    
                    },                  
                }},
                new Countries() { Country = "Canada",  states = new List<States>
                {
                    new States() { State = "BritishColombia" , cities = new List<Cities>
                    {
                        new Cities(){ City = "Vancouver" , areas = null}}

                    },
                    new States() { State = "Ontario", cities = new List<Cities>
                    {
                        new Cities(){ City = "Toronto", areas =null}}                    
                    },                  
                }},

                     new Countries() { Country = "UnitedStates",  states = new List<States>
                {
                    new States() { State = "NewHampshire" , cities = new List<Cities>
                    {
                        new Cities(){ City = "Dover", areas= null}}

                    },
                    new States() { State = "NewJersy", cities = new List<Cities>
                    {
                        new Cities(){ City = "Jersycity", areas=null}}                    
                    },                  
                }},

            };

            this.listCountries.ItemsSource = objCountries;
        }

    }

    public class Countries : INotifyPropertyChanged
    {
        private bool isExpanded1;
        public string Country { get; set; }
        public IList<States> states { get; set; }
        public bool IsExpanded1
        {
            get
            {
                return this.isExpanded1;
            }
            set
            {
                if (this.isExpanded1 != value)
                {
                    this.isExpanded1 = value;
                    this.OnPropertyChanged("IsExpanded");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class States : INotifyPropertyChanged
    {
        public string State { get; set; }
        public IList<Cities> cities { get; set; }

        private bool isExpanded2;
        public bool IsExpanded2
        {
            get
            {
                return this.isExpanded2;
            }
            set
            {
                if (this.isExpanded2 != value)
                {
                    this.isExpanded2 = value;
                    this.OnPropertyChanged("IsExpanded");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Cities : INotifyPropertyChanged
    {
        public string City { get; set; }
        public IList<Areas> areas { get; set; }

        private bool isExpanded3;
        public bool IsExpanded3
        {
            get
            {
                return this.isExpanded3;
            }
            set
            {
                if (this.isExpanded3 != value)
                {
                    this.isExpanded3 = value;
                    this.OnPropertyChanged("IsExpanded");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }



    public class Areas
    {
        public string Area { get; set; }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to implement a code behind function using View Model?

How to animate view from code behind on embedded view button click

Change DataTemplate for Expander header from Code Behind

Display images in grid view in windows phone 8

How to add a new XAML View with code behind

How to implement dynamic multi dimensional grid view(scroll view)?

Create HyperlinkButton with text wrapping in code behind (Windows Phone 8)

How to hide view behind the view

Windows Phone crop view

Manipulating Application Bars in Windows Phone 8.1 XAML from Code Behind

How to implement scroll view in java code for android?

How to change the html of div in Web Forms view with HTML passed from code behind

How to pass data from ViewModel to View code behind in Xamarin.Forms following MVVM?

Lock Windows Phone 8 from code

print the text box value in list view in windows phone 8

Laravel - How to Implement Search from Controller and View

How to bind command in code-behind to view in WPF?

How to access code behind from Tablet/Phone template?

How to add a view behind other view in iOS

How to implement horizontal swipe control in windows phone 8?

How to load asp.net grid view code behind along with the grid view when page loads?

How can I access a JsonResult variable in my code behind file from the javascript of my view? Razor Pages. C#

Windows phone 8: open facebook specific post in app from code

Swift 3 - table view, how to create space from the top of the phone?

How to Load all the media from gallery of phone to the recycler view in the fragment

Exception thrown when trying to remove pivot item (with header template) from code behind windows phone

Begin Storyboard in a UserControl embedded from behind code in Parent.xaml.cs in Windows Phone Application?

Accessing children of second pivot item from code behind with their x:Name returns null in Windows Phone 8.1

Django how to DRY code from view