【问题标题】:WPF C# - multiple CheckBox controls in a DataGrid cellWPF C# - DataGrid 单元格中的多个 CheckBox 控件
【发布时间】:2021-05-22 11:33:38
【问题描述】:

在 DataGrid 控件中,我想添加一个列,其中每个单元格将包含一个复选框控件列表(对于每个单元格,可以有不同数量的具有不同描述的此类复选框)。我不知道如何定义这样一个单元格中包含未定义数量的复选框的列。

请帮忙。

【问题讨论】:

  • 欢迎来到 SO。你试图做什么来解决你的问题?请提供代码示例。
  • 想了想,有了在DataGridTemplate中使用ListBox的想法。
  • 树视图会比数据网格更适合这个吗?

标签: c# wpf list checkbox datagrid


【解决方案1】:

我很好奇这会是什么样子,所以我为你整理了一些东西。希望 cmets 足以将您推向正确的方向。

XAML:

<Window x:Class="multipleCheckBoxControlsDatagrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:multipleCheckBoxControlsDatagrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.Resources>
            <!--DataTemplate for "listbox of checkboxes" column. Checks is a property on the ItemsSource of type DataGridRowData.  Check is a property on the ItemsSource of type DataGridCellData -->
            <DataTemplate x:Key="DataTemplate">
                <ListBox ItemsSource="{Binding Checks}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <CheckBox IsChecked="{Binding Check}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DataTemplate>
        </Grid.Resources>
        <DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding DataGridDataCollection}">
            <DataGrid.Columns>
                <!--Custom column that contains a listbox of checkboxes-->
                <DataGridTemplateColumn Header="Checks" CellTemplate="{StaticResource DataTemplate}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

C#:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace multipleCheckBoxControlsDatagrid
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        // PropertyChanged event so the datagrid get updated when DataGridDataCollection changes
        public event PropertyChangedEventHandler PropertyChanged;

        // the datagrid binds to this collection
        private ObservableCollection<DataGridRowData> _DataGridDataCollection;
        public ObservableCollection<DataGridRowData> DataGridDataCollection
        {
            get => _DataGridDataCollection;
            set
            {
                _DataGridDataCollection = value;
                OnPropertyChanged();
            }
        }

        public MainWindow()
        {
            DataGridDataCollection = new ObservableCollection<DataGridRowData>();
            InitializeComponent();
            this.DataContext = this;
            addChecks();
        }

        // adds data to the DataGrid
        private void addChecks()
        {
            DataGridDataCollection.Add(new DataGridRowData(new List<bool> { true, false, true }));
            DataGridDataCollection.Add(new DataGridRowData(new List<bool> { false, true, false, false, true }));
            DataGridDataCollection.Add(new DataGridRowData(new List<bool> { false, true, false, false, true, false, true }));
        }


        // PropertyChanged event so the datagrid get updated when DataGridDataCollection changes
        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

    // this class represents one row of data in the data grid
    // there is one column in this row named Checks
    // the column is a list of DataGridCellData
    // the listbox in each DataGrid cell binds to this
    public class DataGridRowData
    {
        public List<DataGridCellData> Checks { get; set; }

        public DataGridRowData(List<bool> checks)
        {
            Checks = new List<DataGridCellData>();
            foreach(bool b in checks)
            {
                Checks.Add(new DataGridCellData(b));
            }
        }
    }

    // this class represents one unit of data in the above listbox
    // so each cell in the DataGrid is bound to a ListBox and this class represents one line of data in that listbox
    public class DataGridCellData
    {
        public bool Check { get; set; }
        public DataGridCellData(bool check)
        {
            Check = check;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2016-06-14
    相关资源
    最近更新 更多