I have a cell that needs its value to be set on it just being clicked. It is multibound to different properties.
Where am I supposed to do this? I have been trying
not sure if I'm understanding your question correctly; it looks like you want to access and change the DataGridCell content programmatically. Pls check an example below; I've added a SelectedCellsChanged even hander to the datagrid, it should be triggered every time new cell(s) is selected; having the DataGridCellInfo object you can get access to the DataGridCell object and change its Content.
private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
foreach (DataGridCellInfo cellInfo in dataGrid1.SelectedCells)
{
// this changes the cell's content not the data item behind it
DataGridCell gridCell = TryToFindGridCell(dataGrid1, cellInfo);
if (gridCell!=null) gridCell.Content = "changed!!!";
}
}
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row!=null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex>-1)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
code of GetVisualChild is taken from here
hope it's going to help you, also you maight want to take a look at the BeginEdit of a specific Cell from code behind question on SO. I guess it can also give you some ideas
regards