C# WPF DataGrid复选框列样式

C# WPF DataGrid复选框列样式,c#,wpf,datagrid,styles,cell,C#,Wpf,Datagrid,Styles,Cell,我想用(M x N)个单元格创建一个数据网格。每个单元格应包含一个复选框。 现在,如果选中某个单元格的复选框,我想将该特定单元格的背景色更改为红色 我的问题是,我不知道在运行时会有多少行和列 有人能帮我举个小例子吗?或者告诉我从哪里开始?我在网上搜索了好几天都没有成功:( 谢谢你 Sebastian您可以创建表示行和单元格的类型,并在视图中以编程方式创建一些单元格模板和单元格样式。请参考以下示例 public class Row { public Cell[] Cells { get;

我想用(M x N)个单元格创建一个数据网格。每个单元格应包含一个复选框。 现在,如果选中某个单元格的复选框,我想将该特定单元格的背景色更改为红色


我的问题是,我不知道在运行时会有多少行和列

有人能帮我举个小例子吗?或者告诉我从哪里开始?我在网上搜索了好几天都没有成功:(

谢谢你


Sebastian

您可以创建表示行和单元格的类型,并在视图中以编程方式创建一些单元格模板和单元格样式。请参考以下示例

public class Row
{
    public Cell[] Cells { get; set; }
}

public class Cell : INotifyPropertyChanged
{
    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged(); }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public class ViewModel
{
    public ViewModel()
    {
        const int n = 10;
        const int m = 5;

        List<Row> rows = new List<Row>();
        for (int i = 0; i < n; ++i)
        {
            rows.Add(new Row { Cells = new Cell[m] });
            for (int j = 0; j < m; ++j)
            {
                rows[i].Cells[j] = new Cell();
            }
        }
        Rows = rows;
    }

    public IEnumerable<Row> Rows { get; set; }
}
公共类行
{
公共单元格[]单元格{get;set;}
}
公共类单元格:INotifyPropertyChanged
{
检查私人住宅;
公共场所被检查
{
获取{return\u已检查;}
设置{u isChecked=value;NotifyPropertyChanged();}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)=>
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共类视图模型
{
公共视图模型()
{
常数int n=10;
常数int m=5;
列表行=新列表();
对于(int i=0;i
查看:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Window2ViewModel viewModel = new Window2ViewModel();
        Row row = viewModel.Rows.FirstOrDefault();
        if (row != null)
        {
            DataTemplate dataTemplate = dataGrid.Resources["cellTemplate"] as DataTemplate;
            for (int i = 0; i < row.Cells.Length; ++i)
                dataGrid.Columns.Add(new DataGridTemplateColumn() { CellTemplate = CreateCellTemplate(i), CellStyle = CreateCellStyle(i) });
        }
        DataContext = viewModel;
    }

    private static DataTemplate CreateCellTemplate(int index)
    {
        const string Xaml = " <DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key=\"cellTemplate\"><CheckBox IsChecked=\"{{Binding Cells[{0}].IsChecked, UpdateSourceTrigger=PropertyChanged}}\" Content=\"Check\" /></DataTemplate>";
        return XamlReader.Parse(string.Format(Xaml, index)) as DataTemplate;
    }

    private static Style CreateCellStyle(int index)
    {
        const string Xaml = "<Style TargetType=\"DataGridCell\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"> " +
                            "     <Style.Triggers> " +
                            "       <DataTrigger Binding=\"{{Binding Cells[{0}].IsChecked}}\" Value=\"True\"> " +
                            "        <Setter Property=\"Background\" Value=\"Red\" /> " +
                            "       </DataTrigger> " +
                            "     </Style.Triggers> " +
                            "</Style>";

        return XamlReader.Parse(string.Format(Xaml, index)) as Style;
    }
}
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Rows}" AutoGenerateColumns="False" CanUserAddRows="False" />
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
Window2ViewModel viewModel=新的Window2ViewModel();
Row-Row=viewModel.Rows.FirstOrDefault();
如果(行!=null)
{
DataTemplate DataTemplate=dataGrid.Resources[“cellTemplate”]作为DataTemplate;
对于(int i=0;i
XAML:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Window2ViewModel viewModel = new Window2ViewModel();
        Row row = viewModel.Rows.FirstOrDefault();
        if (row != null)
        {
            DataTemplate dataTemplate = dataGrid.Resources["cellTemplate"] as DataTemplate;
            for (int i = 0; i < row.Cells.Length; ++i)
                dataGrid.Columns.Add(new DataGridTemplateColumn() { CellTemplate = CreateCellTemplate(i), CellStyle = CreateCellStyle(i) });
        }
        DataContext = viewModel;
    }

    private static DataTemplate CreateCellTemplate(int index)
    {
        const string Xaml = " <DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key=\"cellTemplate\"><CheckBox IsChecked=\"{{Binding Cells[{0}].IsChecked, UpdateSourceTrigger=PropertyChanged}}\" Content=\"Check\" /></DataTemplate>";
        return XamlReader.Parse(string.Format(Xaml, index)) as DataTemplate;
    }

    private static Style CreateCellStyle(int index)
    {
        const string Xaml = "<Style TargetType=\"DataGridCell\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"> " +
                            "     <Style.Triggers> " +
                            "       <DataTrigger Binding=\"{{Binding Cells[{0}].IsChecked}}\" Value=\"True\"> " +
                            "        <Setter Property=\"Background\" Value=\"Red\" /> " +
                            "       </DataTrigger> " +
                            "     </Style.Triggers> " +
                            "</Style>";

        return XamlReader.Parse(string.Format(Xaml, index)) as Style;
    }
}
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Rows}" AutoGenerateColumns="False" CanUserAddRows="False" />


此示例尚未完成,但演示了我现在是如何解决的:

此示例未使用数据网格。布局s*cks:D

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<KeyGroup> keygroups = new List<KeyGroup>();
        ColumnHeaderRow headerrow = new ColumnHeaderRow(keygroups);
        for (int i = 0; i <= 10; i++)
        {
            keygroups.Add(new KeyGroup("Col " + i.ToString()));
        }

        VStack.Children.Add(headerrow);

        for (int i = 0; i <= 10; i++){
            List<CellObject> cells = new List<CellObject>();
            for (int j = 0; j <= 10; j++)
            {
                if (i % 2 == 0) cells.Add(new CellObject(i, j, true));
                else cells.Add(new CellObject(i, j, false));
            }
            RowObject newrow = new RowObject(cells);
            VStack.Children.Add(newrow);
        }
    }
}

public class RowObject : StackPanel
{
    public List<CellObject> cells { get; set; }

    public RowObject(List<CellObject> cells)
    {
        this.cells = cells;
        this.Orientation = Orientation.Horizontal;
        foreach(CellObject cell in this.cells)
        {
            this.Children.Add(cell);
        }
    }
}

public class CellObject : CheckBox
{
    public int Row { get; set; }
    public int Column { get; set; }
    public Int64 Id { get; set; }
    public bool Value { get { return this.IsChecked.Value; } set { this.IsChecked = value; } }
    public Int64 IdLck { get; set; }
    public Int64 IdKey { get; set; }
    public SolidColorBrush BackgroundColor { get; set; }


    public CellObject(int row, int column, bool value)
    {
        this.Value = value;
        this.Row = row;
        this.Column = column;

        this.Checked += OnChecked;
        this.Unchecked += OnUnchecked;

        if (this.Value)
        {
            this.Background = new SolidColorBrush(Colors.Red);
            this.Foreground = new SolidColorBrush(Colors.Green);

        }
    }

    private void OnUnchecked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("You unchecked Row: " + this.Row.ToString() + " Column: " + this.Column.ToString());
    }

    private void OnChecked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("You checked Row: " + this.Row.ToString() + " Column: " + this.Column.ToString());
    }
}

public class KeyGroup
{
    public String Name { get; set; }

    public KeyGroup(String name)
    {
        this.Name = name;
    }
}

public class ColumnHeaderRow : StackPanel
{
    public ColumnHeaderRow(List<KeyGroup> keygroups)
    {
        this.Orientation = Orientation.Horizontal;

        foreach (KeyGroup kg in keygroups)
        {
            TextBox tb = new TextBox();
            tb.Text = kg.Name;
            this.Children.Add(tb);
        }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
List keygroups=新列表();
ColumnHeaderRow headerrow=新ColumnHeaderRow(键组);

对于(int i=0;i而不是DataGrid您可以使用一个ItemsControl,其UniformGrid为ItemsPanel,并在ItemTemplate中使用一个复选框。将其ItemsSource属性绑定到具有布尔属性的对象集合。检查此帖子我的问题是我不知道如何布局绑定以选择第m行的第n个单元格。我有一行objec带单元格对象列表的“t”。@Clemens难道我不需要指定统一网格的尺寸吗?即行和列的确切数量?“带单元格对象列表的行对象”可能最好通过嵌套的Items控件显示,该控件具有垂直的外部堆栈面板和水平的内部堆栈面板。只需确保在表示单元格的UI元素上设置固定的宽度。非常感谢您提供的示例!这正是我试图实现的目标。尽管如此,克莱门建议使用两个嵌套的堆栈面板还是不错的我认为这是解决我问题的一种方法。但我认为这应该是这个问题的公认答案。