C# 如何在gridview中使一个项目源跨越多个列

C# 如何在gridview中使一个项目源跨越多个列,c#,wpf,C#,Wpf,我有一个数据源,我想在GridView的两列中显示这个数据源。我添加了可观察的按钮集合作为父ListBox的项目源,所有按钮都在第二列中结束。如何让这三个按钮分别显示在第1列、第2列和下一行的第1列中 xaml: c代码: private IList<Button> buttons = new ObservableCollection<Button>(); public MainWindow() { InitializeComp

我有一个数据源,我想在GridView的两列中显示这个数据源。我添加了可观察的按钮集合作为父ListBox的项目源,所有按钮都在第二列中结束。如何让这三个按钮分别显示在第1列、第2列和下一行的第1列中

xaml:

c代码:

 private IList<Button> buttons = new ObservableCollection<Button>();

        public MainWindow() {
            InitializeComponent();

            Button b = new Button();
            b.Content = "custom Button 1 ";
            buttons.Add(b);

            b = new Button();
            b.Content = "custom button 2 ";
            buttons.Add(b);

            b = new Button();
            b.Content = "custom button 3 ";
            buttons.Add(b);

            listBox.ItemsSource = buttons;
        }

我不认为gridview是一个很好的计划,但无论如何,让我们先来看看它

通常的方法不是向listview显示按钮,而是向listview显示数据,并将您提供给它的内容输入到按钮中。 你可以有这样的东西:

<Window.DataContext>
    <local:MainWIndowViewModel/>
</Window.DataContext>
<Grid>
    <ListView ItemsSource="{Binding Rows}">
        <ListView.View >
            <GridView >
                <GridViewColumn Header="Col 1" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Col1.Content}"
                                    Command="{Binding Col1.Command}"
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Col 2" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Col2.Content}"
                                    Command="{Binding Col2.Commmand}"
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
不使用列,您可以将在没有任何gridview的listbox或listview中显示数据的面板设置为wrappanel,并且只包含一个buttondata集合。

创建一个数据对象视图模型,该模型表示ListView中的一行,即在本例中为具有两个属性的类型:

public class Row
{
    public object Col1 { get; set; }
    public object Col2 { get; set; }
}
将源集合转换为IEnumerable:

XAML:


ItemsControl就是这样工作的,即它为其ItemsSource中的每个对象创建一个可视容器行。

此代码工作。但是,当我选择列表框的第1行时,它同时有按钮1和按钮2作为所选项目,对吗?我希望“选中的行”更像是“选中的行/列”。隐马尔可夫模型。。。
class MainWIndowViewModel
{
    public ObservableCollection<RowVM> Rows { get; set; }
    = new ObservableCollection<RowVM>
    {
        new RowVM
        {
            Col1=new ButtonData{ Content="Button A" },  Col2=new ButtonData{ Content="Button B" }
        },
                    new RowVM
        {
            Col1=new ButtonData{ Content="Button C" },  Col2=new ButtonData{ Content="Button D" }
        },
                     new RowVM
        {
            Col1=new ButtonData{ Content="Button E" } 
        }
    };
}
public class RowVM
{
    public ButtonData Col1 { get; set; }
    public ButtonData Col2 { get; set; }
}

public class ButtonData
{
    public string Content { get; set; }
    public ICommand Command { get; set; }
}
public class Row
{
    public object Col1 { get; set; }
    public object Col2 { get; set; }
}
public MainWindow()
{
    InitializeComponent();

    Button b = new Button();
    b.Content = "custom Button 1 ";
    buttons.Add(b);

    b = new Button();
    b.Content = "custom button 2 ";
    buttons.Add(b);

    b = new Button();
    b.Content = "custom button 3 ";
    buttons.Add(b);

    Row[] rows = new Row[(int)Math.Ceiling(buttons.Count / 2.0)];
    int buttonIndex = 0;
    for (int i = 0; i < rows.Length; ++i)
    {
        rows[i] = new Row();
        rows[i].Col1 = buttons[buttonIndex++];
        if (buttons.Count > buttonIndex)
            rows[i].Col2 = buttons[buttonIndex++];
    }
    listBox.ItemsSource = rows;
}
<ListView x:Name="listBox">
    <ListView.View>
        <GridView x:Name="gridview">
            <GridViewColumn Header="col1" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding Col1}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="col2" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding Col2}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>