简体   繁体   中英

How can I bind each cell of a listView in WPF

I have a listView of 3 rows by 2 columns. I want to bind each cell to a class like {int A; int B; int C; int D; int E; int F;}

So my table will look like

A B
C D
E F

And there won't be any more rows.

This is what I wrote. And my data model won't fit. How can I bind each cell instead of each column?

        <ListView.View>
            <GridView>
                <GridViewColumn Width="175" DisplayMemberBinding="{Binding A}">
                </GridViewColumn>
                <GridViewColumn Width="145" DisplayMemberBinding="{Binding B}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

As far as i know, each row is an instance of a class, so you can't bind data from one instance to different rows. But if you want to make a listview with a static number of rows you can do smth like that.

private ObservableCollection<SimpleClass> _simpleCollection;
        public ObservableCollection<SimpleClass> SimpleCollection
        {
            get { return _simpleCollection ?? (_simpleCollection = new ObservableCollection<SimpleClass>()
            {
                new SimpleClass
                {
                    A = "1", B = "2"
                },
                new SimpleClass
                {
                    C = "3", D = "4"
                },
                new SimpleClass
                {
                    E = "5", F = "6"
                }
            }); }
            set
            {
                _simpleCollection = value;
            }
        }
    public class SimpleClass
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string E { get; set; }
        public string F { get; set; }
    }

And in your View:

<ListView ItemsSource="{Binding SimpleCollection}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="First column">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding A}"></TextBlock>
                                <TextBlock Text="{Binding C}"></TextBlock>
                                <TextBlock Text="{Binding E}"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Second column">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding B}"></TextBlock>
                                <TextBlock Text="{Binding D}"></TextBlock>
                                <TextBlock Text="{Binding F}"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM