0

4 つの textBox から ListView にデータをバインドするプログラムがあります。

XAML コード:

<Window x:Class="TestBindingData.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="324" Width="592">
    <Grid>
        <ListView Height="209" HorizontalAlignment="Left" Margin="12,64,0,0" Name="listView1" VerticalAlignment="Top" Width="546" >
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding STT}" Header="STT" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding HVT}" Header="Name" Width="200"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding State}" Header="State" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox Height="22" HorizontalAlignment="Left" Margin="99,25,0,0" Name="textBox1" VerticalAlignment="Top" Width="45" />
        <TextBox Height="22" HorizontalAlignment="Left" Margin="150,25,0,0" Name="textBox2" VerticalAlignment="Top" Width="138" />
        <TextBox Height="22" HorizontalAlignment="Left" Margin="294,25,0,0" Name="textBox3" VerticalAlignment="Top" Width="45" />
        <TextBox Height="22" HorizontalAlignment="Left" Margin="345,25,0,0" Name="textBox4" VerticalAlignment="Top" Width="92" />
        <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="461,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

メインウィンドウ コード:

namespace TestBindingData
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        class person
        {
            public string STT { get; set; }
            public string HVT { get; set; }
            public string Age { get; set; }
            public string State { get; set; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            List<person> people = new List<person>();
            people.Add(new person() { STT = textBox1.Text.ToString(), HVT = textBox2.Text.ToString(), Age = textBox3.Text.ToString(), State = textBox4.Text.ToString() });
            this.listView1.ItemsSource = people;
        }
    }
}

[追加] ボタンをクリックすると、プログラムは人々の情報を含む行を追加します。しかし、行 1 の後に行 2、行 3、... を追加することはできません。助けてください、ありがとう!

4

3 に答える 3

0

これを試してください->

    public partial class MainWindow : Window
        {
            private ObservableCollection<person> _people;

            public ObservableCollection<person> people
            {
                get { return _people; }
                set { _people = value; }
            }
            public MainWindow()
            {
InitializeComponent();
                people = new ObservableCollection<person>();
            }
           public class person
            {
                public string STT { get; set; }
                public string HVT { get; set; }
                public string Age { get; set; }
                public string State { get; set; }
            }

            public void button1_Click(object sender, RoutedEventArgs e)
            {           
                people.Add(new person() { STT = textBox1.Text.ToString(), HVT = textBox2.Text.ToString(), Age = textBox3.Text.ToString(), State = textBox4.Text.ToString() });
                this.listView1.ItemsSource = people;
            }

        }

これは動作します..:)

于 2013-07-29T13:55:49.880 に答える