我为数据表创建了简单的机制xaml看起来很简单:
<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" AutoGenerateColumns="True" />
后面的MVVM代码基于数据表,而且相当简单:
private void GenerateDataView()
{
CurrentsFlagValuesView = new DataTable();
CurrentsFlagValuesView.Columns.Add("Bits");
var bitLength = 0;
foreach (CurrentsFlagAnalysis flag in CurrentsFlagValues)
{
CurrentsFlagValuesView.Columns.Add(flag.DailyCurrentsTimestampInterval.ToString("yyyy-MM-dd"));
bitLength = flag.CurrentFlagsLength;
}
for (var bit = 0; bit < bitLength; bit++)
{
List<CurrentFlagEventEnum> flags = CurrentsFlagValues
.Select(value => value.CurrentFlags.ElementAt(bit))
.Select(value => value ? (CurrentFlagEventEnum)bit + 1 : CurrentFlagEventEnum.None)
.ToList();
var dataRowValues = new List<object> { bit };
dataRowValues.AddRange(flags.Cast<object>());
CurrentsFlagValuesView.Rows.Add(dataRowValues.ToArray());
}
}
但是当我点击单元格(如列标题和单元格的值)时,我遇到了一个问题或者两个我想获取单元格数据的问题。我设法在没有MVVM的情况下这样做:
void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCell Cell = EditingDataGrid.GetCurrentDataGridCell();
var Position = Cell.PointToScreen(new Point(0, 0));
TextBlock text = (TextBlock)Cell.Content;
MessageBox.Show("Value=" + text.Text, "Position" );
}
public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)
{
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if (cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
但现在我想改造那种模式,但我不知道如何。任何想法如何绑定命令?
您可以将CurrentCell
的DataGrid
属性绑定到DataGridCellInfo
(而不是DataGridCell
)源属性,前提是您设置Mode
的Binding
两个TwoWay
:
<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}"
CurrentCell="{Binding CurrentCell, Mode=TwoWay}"
AutoGenerateColumns="True" />
然后,只要在视图中选择单元格,就会设置视图模型的source属性,您只需将当前逻辑移动到视图模型:
private DataGridCellInfo _currentCell;
public DataGridCellInfo CurrentCell
{
get { return _currentCell; }
set { _currentCell = value; OnCurrentCellChanged(); }
}
void OnCurrentCellChanged()
{
DataGridCell Cell = GetCurrentDataGridCell(_currentCell);
var Position = Cell.PointToScreen(new Point(0, 0));
TextBlock text = (TextBlock)Cell.Content;
MessageBox.Show("Value=" + text.Text, "Position");
}
public static DataGridCell GetCurrentDataGridCell(DataGridCellInfo cellInfo)
{
if (cellInfo == null || cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
您还可以将此功能包装在将视图模型的source属性设置为actuall单元格值的行为中:
https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/
您可以简单地绑定视图模型中的当前单元格属性,您将始终拥有当前单元格:
<DataGrid AutoGenerateColumns="True"
SelectionUnit="Cell"
CanUserDeleteRows="True"
ItemsSource="{Binding Results}"
CurrentCell="{Binding CellInfo}"
SelectionMode="Single">
在视图模型中:
private DataGridCell cellInfo;
public DataGridCell CellInfo
{
get { return cellInfo; }
}