I am developing a WPF application that uses a datagrid to display a list of objects. A few of the columns are displaying properties with deeper information, I would like to display a summary value in the data grid, and then display a deeper window when clicked to allow editing properties.
I am part way to what I want, in that my data grid displays what I want and that I can make a dialog box appear when one of my special columns are clicked.
<DataGrid ItemsSource="{Binding Sequence.Steps}" AutoGenerateColumns="False" IsReadOnly="True" SelectionUnit="Cell">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseDown" Handler="DataGrid_PreviewMouseDown" />
<Setter Property="Focusable" Value="False" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Axis 1" Binding="{Binding Axes[0]}" ClipboardContentBinding="{Binding Axes[0].DestinationSP}" Width="*" />
<DataGridTextColumn Header="Axis 2" Binding="{Binding Axes[1]}" ClipboardContentBinding="{Binding Axes[1].DestinationSP}" Width="*"/>
<DataGridTextColumn Header="Axis 3" Binding="{Binding Axes[2]}" ClipboardContentBinding="{Binding Axes[2].DestinationSP}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
I have then defined a handler
private void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var dlg = new Views.EditAxisMovement(CellValue); // Need to find a way to get Cell value so I can pass it
dlg.Owner = this;
dlg.ShowDialog();
}
However I cant find the right way to find CellValue
to pass to the dialog box. Is there a nice and/or better way of handling this.
You could create a DataGridCellInfo
and check the header of the column to map the cell against a property of your data item, e.g.:
private void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = (DataGridCell)sender;
DataGridCellInfo cellInfo = new DataGridCellInfo(cell);
string header = cellInfo.Column.Header?.ToString();
int index = -1;
switch (header)
{
case "Axis 1":
index = 0;
break;
case "Axis 2":
index = 1;
break;
case "Axis 3":
index = 2;
break;
}
if (index != -1)
{
}
var dataItem = cellInfo.Item as YourClass;
object value = dataItem.Axes[index];
//...
}