如何从联系人添加数据并动态显示在列表框中 [英] How to add data from Contact and Display it in ListBox dynamically

查看:20
本文介绍了如何从联系人添加数据并动态显示在列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求如下
1) 从联系人中选择联系人(选择一个或多个)
2) 在列表框中添加所选联系人的手机号码和联系人姓名
3)删除:在ListBox中选择一个联系人,动态删除并刷新数据

我的问题:
a) 如何将联系人的数据绑定到 ListBox?

<前><代码>PhoneCtc = new PhoneNumberChooserTask();PhoneCtc.Completed += new EventHandler(PhoneCtc_Complete);public void PhoneCtc_Complete(对象发送者,PhoneNumberResult e){字符串 No = e.PhoneNumber;字符串名称 = e.DisplayName;//如何将此数据动态添加到列表框并显示?}

谢谢

解决方案

定义列表框中项目的DataTemplate如下图:

<ListBox.ItemTemplate><数据模板><StackPanel Orientation="Horizo​​ntal" Height="132"><TextBlock Text="{绑定号}"/><TextBlock Text="{绑定名称}"/></StackPanel></数据模板></ListBox.ItemTemplate></列表框>

然后您必须创建一个包含 No 和 Name 的 Contact 类:

公开课联系方式{公共字符串 否 { 获取;放;}公共字符串名称{获取;放;}}

现在在代码中添加元素时,只需要初始化一个实例类Contact并设置变量然后写listBox1.Items.Add(联系);

My requirement as follows
1) Select the contact from the Contact ( select one or many)
2) Add the Mobile no and Contact name from selected contact in ListBox
3) To delete : Select a contact in the ListBox and delete dynamically and refresh data

My Problems:
a) How to I bind data from contact to ListBox?



 PhoneCtc = new PhoneNumberChooserTask();

PhoneCtc.Completed += new EventHandler(PhoneCtc_Complete);


public void PhoneCtc_Complete(object sender, PhoneNumberResult e)
        {

          string No     = e.PhoneNumber;
          string Name      = e.DisplayName;

// how to add this data dynamically to listbox and display ?

 }

Thanks

解决方案

Define the DataTemplate of the items in the listbox as shown below:

<ListBox Name="listBox1">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal" Height="132">
        <TextBlock Text="{Binding No}"/>
        <TextBlock Text="{Binding Name}"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>  

Then you'll have to create a class Contact containing No and Name:

public class Contact
    {
        public string No { get; set; }
        public string Name { get; set; }
    }

Now in code when you want to add an element, you just need to initialize an instance class Contact and set variables then write listBox1.Items.Add(Contact);

这篇关于如何从联系人添加数据并动态显示在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆