简体   繁体   中英

Trouble binding ObservableCollection to DataTemplate

I'm trying to build a simple app to display the results of an API call. I can't seem to figure out how to bind my results to my data template.

The behavior i am looking for in this TestApp is that when the button is pushed the listview displays the name, party, and state of each Representative.

namespace TestApp
{        
    public class Reps
    {
        public Rep[] results { get; set; }
    }

    public class Rep
    {
        public string name { get; set; }
        public string party { get; set; }
        public string state { get; set; }
        public string district { get; set; }
        public string phone { get; set; }
        public string office { get; set; }
        public string link { get; set; }

        public Rep() { }

        public Rep(string name, string party, string state)
        {
            this.name = name;
            this.party = party;
            this.state = state;
        }
    }      
}

namespace TestApp
{    
    public sealed partial class MainPage : Page
    {
        public ObservableCollection<Rep> Representative = new ObservableCollection<Rep>();

        public MainPage()
        {
            this.InitializeComponent();            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            listView1.DataContext = Representative;
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {            
            var uri = new Uri("http://whoismyrepresentative.com/getall_mems.php?zip=31023&output=json");
            var request = WebRequest.CreateHttp(uri);
            request.Method = "GET";
            request.Accept = "application/json";
            var response = await request.GetResponseAsync();
            var stream = response.GetResponseStream();

            var deserializer = new DataContractJsonSerializer(typeof(Reps));

            Reps rps = (Reps)deserializer.ReadObject(stream);

            for (int i = 0; i < rps.results.Length; i++)
            {             
                Rep newRep = new Rep { name = rps.results[i].name.ToString(), 
                                       party = rps.results[i].party.ToString(), 
                                       state = rps.results[i].state.ToString() };
                Representative.Add(newRep);

                Debug.WriteLine(rps.results[i].name.ToString());
            }                      
        }
    }
}

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Button" 
            HorizontalAlignment="Left" 
            Margin="275,212,0,0" 
            VerticalAlignment="Top" 
            Click="Button_Click" />
        <Grid HorizontalAlignment="Left" 
              Height="293" 
              Margin="524,133,0,0" 
              VerticalAlignment="Top" 
              Width="399">             
            <ListView x:Name="listView1" ItemsSource="{Binding Representative}">
                <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding name}"
                               FontSize="20"
                               Foreground="White"/>
                            <TextBlock Text="{Binding party}"
                                       FontSize="20"
                                       Foreground="White"/>
                            <TextBlock Text="{Binding state}"
                                       FontSize="20"
                                       Foreground="White"/>
                    </StackPanel>
                </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>

Setting the DataContext of the ListView only sets the context object for bindings, but it is not a binding on its own. Thus, just replace the line

listView1.DataContext = Representative;

with

listView1.ItemsSource = Representative;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM