I have a WPF datagrid which uses binding to a list of objects. The object has a Name property which the user should be able to change, default value is "Add name here".
The behavior I need to implement is: Clear the cell when the user clicks into it, then if the user leaves it blank, return to the default value.
I have a function which allows the user to single-click edit, which works fine to clear the cell (partial code below). What I can't do is get the "Add name here" text back into the cell if the cell loses focus.
I feel like I've tried everything I can find on Google. I've tried validators, where I set value = "Add name here" if value == null, but that doesn't work. Also, something about the code below prevents the validator from working, even in applying the default red border (I am familiar with validators and they do work successfully in other parts of my code).
I tried using TargetNullValue, but that doesn't allow the cell to be blank at any time, and I do want the cell to be blank when it's initially clicked on. Any ideas?
//Allows user to click on "Name" field once to edit
private void SingleClickEdit(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
DataGridCellInfo cellInfo = new DataGridCellInfo(cell);
System.Reflection.PropertyInfo pi = cellInfo.Item.GetType().GetProperty("controllerName");
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused)
{
cell.Focus();
if (pi != null)
{
pi.SetValue(cellInfo.Item, null);
}