如何从windows phone 8中的代码后面的多级(树视图)实现Expander视图?

时间:2015-02-19 05:13:05

标签: c# xaml windows-phone-8

我是通过在xaml中使用静态记录来完成的,并且完美地工作,在这个例子中我添加了三个级别的扩展器视图,我的整个xaml代码在下面

<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

我想从后面的代码中绑定相同的细节。在此先感谢:)

2 个答案:

答案 0 :(得分:0)

你可以这样做:

 //Header1 is the name of your Expander View
 Header1.Header = "Expander Header"; 
 stackpanel.Children.Add(Header1);  
 this.Header1.ItemsSource = new List<string>() { "Afganistan","Australia", "Bangladesh", "Ireland", "New Zealand", "South Africa", "Sri Lanka", "Zimbabwe" };

参考:Defining ExpanderView Control using CSharp

对于Data Binding,您可以查看this

答案 1 :(得分:0)

这是我的问题的解决方案。

Xaml代码:

<!--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代码:

 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; }
    }