Może zrób podświetlanie item-a po utracie focus-a przez DataGrid bez używania zdarzenia LostFocus, ale za pomocą <Style.Triggers>
przykład
MainWindow.xaml
<Window x:Class = "WPFDataGridControl.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "348" Width = "279">
<Grid Width="502">
<DataGrid Name = "dataGrid" AlternatingRowBackground = "LightBlue"
AlternationCount = "2" AutoGenerateColumns = "False" Margin="12,12,251,134">
<DataGrid.Columns>
<DataGridTextColumn Header = "Name" Binding = "{Binding Name}" />
<DataGridTextColumn Header = "Surname" Binding = "{Binding Surname}" />
<DataGridCheckBoxColumn Header = "Is on holiday?" Binding = "{Binding IsOnHoliday}" />
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
<Setter Property="BorderBrush" Value="Darkgreen" />
<Setter Property="TextElement.Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
<TextBox Height="27" HorizontalAlignment="Left" Margin="12,181,0,0"
Name="textBox" VerticalAlignment="Top" Width="239" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WPFDataGridControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string PlaceholderText = "Zaznacz wiersz w DG i wstaw tu kursor";
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = Employee.GetEmployees();
textBox.Text = PlaceholderText;
textBox.LostFocus += AddPlaceholder;
textBox.GotFocus += RemovePlaceholder;
}
public void RemovePlaceholder(object sender, EventArgs e)
{
if (textBox.Text == PlaceholderText)
textBox.Text = String.Empty;
}
public void AddPlaceholder(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox.Text))
textBox.Text = PlaceholderText;
}
}
public class Employee : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
RaiseProperChanged();
}
}
private string surname;
public string Surname
{
get { return surname; }
set
{
surname = value;
RaiseProperChanged();
}
}
private bool isOnHoliday;
public bool IsOnHoliday
{
get { return isOnHoliday; }
set
{
isOnHoliday = value;
RaiseProperChanged();
}
}
public static ObservableCollection<Employee> GetEmployees()
{
var employees = new ObservableCollection<Employee>();
employees.Add(new Employee()
{
Name = "Mariusz",
Surname = "Kowalski",
IsOnHoliday = true
});
employees.Add(new Employee()
{
Name = "Jacek",
Surname = "VBService",
IsOnHoliday = false
});
employees.Add(new Employee()
{
Name = "Katarzyna",
Surname = "Mazur",
IsOnHoliday = true
});
employees.Add(new Employee()
{
Name = "Tomasz",
Surname = "Lewandowski",
IsOnHoliday = false
});
employees.Add(new Employee()
{
Name = "Kamil",
Surname = "Zamojski",
IsOnHoliday = true
});
employees.Add(new Employee()
{
Name = "Magdalena",
Surname = "Piasecka",
IsOnHoliday = false
});
return employees;
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaiseProperChanged([CallerMemberName] string caller = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
}