0

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; }
    }

}

この My Xaml: Name、FamilyName、phoneNumber の 3 つの textBox があります。新しい連絡先を作成するための listBox とボタン

<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;}

    }
}
4

1 に答える 1

1

こんにちはまず第一に、コーディングする前にバインディングと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));
    }
}

これがアイデアを与えるのに役立つことを願っています。

于 2013-02-03T14:22:11.447 に答える