[英]WPF DataGrid Checkbox Column Style
I want to create a datagrid with (M x N) cells. 我想用(M x N)个单元创建一个数据网格。 Each cell shall contain a Checkbox. 每个单元应包含一个复选框。 Now if the checkbox of one cell is selected, I want to change the background color of this specific cell to red. 现在,如果选中一个单元格的复选框,我想将此特定单元格的背景色更改为红色。
My problem is that I don't know how many rows and columns will be there at runtime. 我的问题是我不知道运行时将有多少行和多少列。
Can someone please help me with a little example? 有人可以帮我举个例子吗? Or give me a hint where to start? 还是给我提示从哪里开始? I searched the web for days now without any success :( 我在网上搜索了几天,但没有成功:(
Thank's! 谢谢!
Sebastian 塞巴斯蒂安
You could create types that represents a row and a cell and create some cell templates and cell styles programmatically in the view. 您可以创建代表行和单元格的类型,并在视图中以编程方式创建一些单元格模板和单元格样式。 Please refer to the following example. 请参考以下示例。
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; }
}
View: 视图:
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;
}
}
XAML: XAML:
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Rows}" AutoGenerateColumns="False" CanUserAddRows="False" />
This example is not finished yet but demonstrates how I solved it now: 该示例尚未完成,但演示了我现在如何解决它:
This example is not using a DataGrid. 本示例未使用DataGrid。 And the layout s*cks :D 和布局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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.