none
ListView 怎么增加行数据? RRS feed

  • 问题

  •     <Grid>
            <ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="AAA"/>
                        <GridViewColumn Header="BBB"/>
                        <GridViewColumn Header="CCC"/>
                    </GridView>
                </ListView.View>
                <ListViewItem>aaa/bbb/ccc</ListViewItem>
                <ListViewItem>aaaa</ListViewItem>
                <ListViewItem>aaaaa</ListViewItem>
            </ListView>
        </Grid>

    1、<ListViewItem>aaa/bbb/ccc</ListViewItem> 要怎么写才能在三列中显示数据,如:

    AAA   BBB   CCC

    aaa   bbb   ccc

    2、用代码怎么写,如
             ListViewItem lvi=new ListViewItem();
             this.listView1.Items.Add(lvi);

    不考虑使用DataTable  this.myListView.DataContext = DataTable;

     DataContext = new[]
        {
            new Data{No = 1, Text = "ABC"},
            new Data{No = 2, Text = "DEF"},
        };

        public class Data
        {
            public int No { get; set; }
            public string Text { get; set; }
        }

     

    哇咔咔,谢谢肖哥。
    Person a = new Person
                {
                    Name = "aaa",
                    ID = "bbb",
                    City = "ccc"
                };
                ListViewItem temp = new ListViewItem();
                temp.Content = a;
                temp.IsSelected = true;
                listView1.Items.Add(temp);
    2011年3月18日 6:33

答案

  • Xaml:

    <ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top">

        <ListView.View>

            <GridView>

                <GridViewColumn Header="AAA" DisplayMemberBinding="{Binding Name}"/>

                <GridViewColumn Header="BBB" DisplayMemberBinding="{Binding ID}"/>

                <GridViewColumn Header="CCC" DisplayMemberBinding="{Binding City}"/>

            </GridView>

        </ListView.View>

    </ListView>

    code-behind:

    public partial class MainWindow : Window

    {

        private ObservableCollection<Person> Persons = new ObservableCollection<Person>();

        public MainWindow()

        {

            InitializeComponent();

            this.Persons.Add(new Person() { Name = "aaa", ID="bbb",City="ccc" });

            listView1.ItemsSource = Persons;

        }

    }

     

    public class Person

    {

        public string Name { get; set; }

        public string ID { get; set; }

        public string City { get; set; }

    }

     

     

     

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年3月18日 6:47
    版主