【问题标题】:Address Book and I want to see My contact On a List Box, How Can I Bind通讯录,我想在列表框中查看我的联系人,我该如何绑定
【发布时间】:2013-01-18 05:11:21
【问题描述】:

我正在尝试在 WPF 中创建通讯簿。 如何将我的联系人数据绑定到列表框? 这是我的联系人类:

public class Contact
{

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }



    private string familyname;

    public string FamilyName
    {
        get { return familyname; }
        set { familyname = value; }
    }



    private string phonenumber;

    public string PhoneNumber
    {
        get { return phonenumber; }
        set { phonenumber = value; }
    }

}

这个我的 Xaml: 我有 3 个用于姓名、家庭姓名和电话号码的文本框;一个列表框和一个用于创建新联系人的按钮

<Window x:Class="PhoneBookTest10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AddressBook" Height="350" Width="525" FontStyle="Italic">
    <Grid>
        <ListBox Height="188" HorizontalAlignment="Left" Margin="25,28,0,0" Name="listBox1" VerticalAlignment="Top" Width="197" />
        <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="259,64,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,69,28,0" Name="textBox1" VerticalAlignment="Top" Width="120" DataContext="{Binding}" />
        <Label Content="FamilyName" Height="28" HorizontalAlignment="Left" Margin="259,121,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox DataContext="{Binding}" Height="23" HorizontalAlignment="Right" Margin="0,126,28,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
        <Label Content="PhoneNumber" Height="28" HorizontalAlignment="Left" Margin="259,188,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox DataContext="{Binding}" Height="23" HorizontalAlignment="Right" Margin="0,193,28,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
        <Button Content="Create New Contact" Height="32" HorizontalAlignment="Left" Margin="25,226,0,0" Name="button1" VerticalAlignment="Top" Width="197" />
    </Grid>
</Window>

这是我的主窗口:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        contacts.Add(new Contact()
        {
            Name = "James",
            FamilyName = "mangol",
            PhoneNumber = "01234 111111"
        });
        contacts.Add(new Contact()
        {
            Name = "Bob",
            FamilyName = "angol",
            PhoneNumber = "01234 222222"
        });
        contacts.Add(new Contact()
        {
            Name = "Emma",
            FamilyName = "pangol",
            PhoneNumber = "01234 333333"
        });
    }
    protected List<Contact> contacts = new List<Contact>();

    public List<Contact> Contacts 
    {
        get{return contacts;}

        set{ contacts = value;}

    }
}

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您好,首先我想说您在编写代码之前需要了解 Binding 和 MVVM。从您的代码来看,您似乎对此没有太多了解。所以在实施之前先探索一下事情

    <Grid>
        <ListBox ItemsSource="{Binding Contacts}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="5"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="5"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="5"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Grid.Column="0" Content="Name"/>
                        <Label Grid.Row="2" Grid.Column="0" Content="FamilyName"/>
                        <Label Grid.Row="4" Grid.Column="0" Content="PhoneNumber"/>
                        <TextBox Grid.Row="0" Grid.Column="2" Text="{Binding Name}"/>
                        <TextBox Grid.Row="2" Grid.Column="2" Text="{Binding FamilyName}"/>
                        <TextBox Grid.Row="4" Grid.Column="2" Text="{Binding PhoneNumber}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    
        public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Contacts = new ObservableCollection<Contact>();
            Contacts.Add(new Contact()
            {
                Name = "James",
                FamilyName = "mangol",
                PhoneNumber = "01234 111111"
            });
            Contacts.Add(new Contact()
            {
                Name = "Bob",
                FamilyName = "angol",
                PhoneNumber = "01234 222222"
            });
            Contacts.Add(new Contact()
            {
                Name = "Emma",
                FamilyName = "pangol",
                PhoneNumber = "01234 333333"
            });
        }
        public ObservableCollection<Contact> Contacts {get;set;}
    
    
    }
    public class Contact:INotifyPropertyChanged
    {
    
        private string name;
    
        public string Name
        {
            get { return name; }
            set { name = value; Notify("Name"); }
        }
    
    
    
        private string familyname;
    
        public string FamilyName
        {
            get { return familyname; }
            set { familyname = value;Notify("FamilyName"); }
        }
    
    
    
        private string phonenumber;
    
        public string PhoneNumber
        {
            get { return phonenumber; }
            set { phonenumber = value; Notify("PhoneNumber"); }
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
    

    我希望这会帮助你给出一个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-12
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多