江户川柯南SVIP 2016-11-28 05:04 采纳率: 0%
浏览 2077
已结题

修改MainWindow.xaml.cs(图片2)的代码使其除了可以根据姓名还能根据电话号码E-mail和通信地址查找联系人

Contact.cs

 using System;
using System.Collections;
using System.Collections.Generic;

namespace P11_5
{
    using Email = System.Tuple<string, string>;
    using Address = System.Tuple<string, string, string, string, int>;

    public interface IItems<T>
    {
        T this[int index] { get; set; }
    }

    public class Contact : IItems<string>, IItems<Email>, IItems<Address>
    {
        private string name;
        public string Name
        {
            get { return name; }
        }

        public bool Gender { get; set; }
        public char GenderChar
        {
            get { return Gender ? '男' : '女'; }
        }

        private string[] phones;
        public string this[int index]
        {
            get { return phones[index]; }
            set { phones[index] = value; }
        }

        private Email[] emails;
        Email IItems<Email>.this[int index]
        {
            get { return emails[index]; }
            set { emails[index] = value; }
        }
        public Email FirstEmail
        {
            get { return emails[0]; }
        }

        private Address[] addresses;
        Address IItems<Address>.this[int index]
        {
            get { return addresses[index]; }
            set { addresses[index] = value; }
        }
        public Address FirstAddress
        {
            get { return addresses[0]; }
        }

        public Contact(string name, bool gender = true)
        {
            this.name = name;
            this.Gender = gender;
            phones = new string[5];
            emails = new Email[3];
            addresses = new Address[3];
        }
    }
}

MainWindow.xaml.cs

 using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace P11_5
{
    using Email = System.Tuple<string, string>;
    using Address = System.Tuple<string, string, string, string, int>;

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Contact> contacts;

        public MainWindow()
        {
            InitializeComponent();
            contacts = new List<Contact>();
            contacts.Add(new Contact("张大强"));
            contacts[0][0] = "13011110234";
            ((IItems<Email>)contacts[0])[0] = new Email("dqzhang", "soft.cn");
            contacts.Add(new Contact("张珊", false));
            contacts[1][0] = "18901890189";
            contacts.Add(new Contact("王小明"));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (Contact c in contacts)
                listView1.Items.Add(c);
        }

        private void textBox1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            listView1.Items.Clear();
            List<Contact> cs = (textBox1.Text == "") ? contacts : contacts.FindAll(c => c.Name.Contains(textBox1.Text));
            foreach (Contact c in cs)
                listView1.Items.Add(c);




        }
    }
}

MainWindow.xaml

 <Window x:Class="P11_5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="240" Width="640" FontSize="15" Loaded="Window_Loaded">
    <StackPanel>
        <WrapPanel>
            <Label Content="查找联系人:" />
            <TextBox Name="textBox1" Width="120" Margin="3" TextChanged="textBox1_TextChanged" />
        </WrapPanel>
    <ScrollViewer>
        <ListView Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="姓名" Width="60" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="性别" Width="50" DisplayMemberBinding="{Binding GenderChar}" />
                    <GridViewColumn Header="电话" Width="90" DisplayMemberBinding="{Binding [0]}" />
                    <GridViewColumn Header="Email" Width="150" DisplayMemberBinding="{Binding FirstEmail}" />
                    <GridViewColumn Header="Address" Width="240" DisplayMemberBinding="{Binding FirstAddress}" />
                </GridView>
            </ListView.View>
        </ListView>
    </ScrollViewer>
    </StackPanel>
</Window>

图片说明
图片说明
图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • devmiao 2017-01-29 17:58
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿