如何在WPF中绑定listView的每个单元格?

本文关键字:单元格 listView 绑定 WPF | 更新日期: 2023-09-27 17:49:34

我有一个3行2列的listView。我想把每个单元绑定到一个类{int;int B;int C;int D;int E;int F;}

所以我的表看起来像

A B
C D
E F

不会有更多的行。

这是我写的。我的数据模型就不合适了。我如何绑定每个单元格而不是每个列?

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

如何在WPF中绑定listView的每个单元格?

据我所知,每一行都是一个类的实例,所以你不能将数据从一个实例绑定到不同的行。但如果你想让listview的行数是静态的你可以这样做。

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>