繁体   English   中英

如何在Windows Phone C#中绑定列表框

[英]How to bind listbox in windows phone c#

在我的Windows Phone应用程序中,我想获取联系人列表,然后单击此链接http://stackoverflow.com/questions/18387249/selecting-contacts-in-windows-phone-8 ,其工作方式如下:

public class CustomContact
{
   public string Name { get; set; }
   public string Number { get; set; }

   public CustomContact()
    {
    }


    public CustomContact(Contact contact)
    {
        Name = contact.DisplayName;
        var number = contact.PhoneNumbers.FirstOrDefault();
        if(number != null)
            Number = number.PhoneNumber;
        else
            Number = "";
    }
}

在上面的代码中,我正在创建CustomContact的类,下面是xaml.cs页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        private void ButtonContacts_Click(object sender, RoutedEventArgs e)
        {
            Contacts cons = new Contacts();
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);

            cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
        }
        void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
           // MessageBox.Show(e.Results.Count().ToString());
            try
            {
List<CustomContact> listOfContacts = new List<CustomContact>();
foreach (var c in e.Results)
{
    CustomContact contact  = new CustomContact();
    contact.DisplayName = c.DisplayName;
    var number = c.PhoneNumbers.FirstOrDefault(); //change this to whatever number you want
    if (number != null)
        contact.Number = number.PhoneNumber;
    else
        contact.Number = "";
            }
            catch (System.Exception)
            {
                //No results
            }

            if (ContactResultsData.Items.Any())
            {
                ContactResultsLabel.Text = "results";
            }
            else
            {
                ContactResultsLabel.Text = "no results";
            }
    listOfContacts.Add(contact);
}
ContactResultsData.DataContext = listOfContacts;

XAML页面

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

        <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <Button x:Name="ButtonContacts"
            Content="Get All Contacts"
            FontSize="15"
            Width="200"
            Height="70"
            Background="AliceBlue"
            Foreground="Blue"
            HorizontalAlignment="Left"
            Click="ButtonContacts_Click"></Button>
</Grid>

但是,当我将listOfContacts放入列表框ContactResultsData它不在列表框中显示联系人,而当我在该行上放置断点ContactResultsData.DataContext = listOfContacts该联系人已经存在于listOfContacts那么为什么它不在列表框ContactResultsData显示。 请建议我,等待您的回复。 谢谢

我的建议是将listOfContacts公开给公共财产。 ObservableCollection其类型更改为ObservableCollection 像这样更改XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <TextBlock Name="ContactResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />

        <ListBox Name="ContactResultsData" ItemsSource="{Binding ContactList}" Height="200" Margin="24,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <Button x:Name="ButtonContacts"
            Content="Get All Contacts"
            FontSize="15"
            Width="200"
            Height="70"
            Background="AliceBlue"
            Foreground="Blue"
            HorizontalAlignment="Left"
            Click="ButtonContacts_Click"></Button>
</Grid>

在代码背后,您应该仅通过删除和添加项目来对联系人公共属性进行操作-请勿更改对其的引用,因为这会破坏XAML与代码之间的绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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