I tried DateTaken_PreviewKeyDown and LoanDate_TextBox_PreviewKeyDown in the EventSetter of CellStyle , without success. You could try by binding the TextBox with the PreviewKeyDown event to the DataGridTemplateCloumn. I commented the relevant code for App.EnumLanguage because I don't know its definition.
MainWindow.xaml:
<Window.Resources>
<CollectionViewSource x:Key="CountryView" Source="{Binding Countries}"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="BookDataGrid" Width="400" Height="250" ItemsSource="{Binding Source={StaticResource CountryView}}"
SelectedItem="{Binding SelectedCountry}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="name" Binding="{Binding Country}"/>
<DataGridTemplateColumn Header="daten" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DateTaken}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<TextBox x:Name="DataGridTextColumnValue" Text="{Binding DateTaken}" PreviewKeyDown="LoanDate_TextBox_PreviewKeyDown"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace PreviewKeyDownDataGridCell
{
public partial class MainWindow : Window
{
ViewModel vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = vm;
}
private void LoanDate_TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var LoanDate_TextBox = sender as TextBox;
switch (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Back || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Tab || Keyboard.Modifiers == ModifierKeys.Control))
{
case true:
e.Handled = true;
break;
default:
switch (e.Key == Key.A || e.Key == Key.C || e.Key == Key.V || e.Key == Key.Y || e.Key == Key.Z)
{
case true:
break;
}
switch (e.Key != Key.Back && e.Key != Key.Home && e.Key != Key.End && e.Key != Key.Left && e.Key != Key.Right)
{
case true:
byte j = 0;
for (byte i = 0; i < LoanDate_TextBox.Text.Length; i++)
{
switch (LoanDate_TextBox.Text[i] == '/')
{
case true:
j++;
break;
}
}
switch (j < 2)
{
case true:
switch (LoanDate_TextBox.Text.Length)
{
case 2:
LoanDate_TextBox.Text = '/' + LoanDate_TextBox.Text;
break;
case 5:
LoanDate_TextBox.Text = '/' + LoanDate_TextBox.Text;
break;
}
break;
}
break;
default:
//switch (e.Key)
//{
// case Key.Back:
// switch (App.EnumLanguage)
// {
// case AllLanguage.Persian:
// switch (LoanDate_TextBox.Text.Length)
// {
// case 3:
// LoanDate_TextBox.SelectionStart = 3;
// break;
// case 6:
// LoanDate_TextBox.SelectionStart = 6;
// break;
// }
// break;
// }
// break;
//}
break;
}
break;
}
}
}
public class ViewModel
{
public ObservableCollection<MyCountry> Countries { get; set; }
public MyCountry SelectedCountry { get; set; }
public ViewModel()
{
Countries = new ObservableCollection<MyCountry>();
Countries.Add(new MyCountry() { Country = "Beijing", DateTaken = DateTime.Now });
Countries.Add(new MyCountry() { Country = "NewYork", DateTaken = DateTime.Now });
Countries.Add(new MyCountry() { Country = "Shanghai", DateTaken = DateTime.Now });
}
}
public class MyCountry
{
public string Country { get; set; }
public DateTime DateTaken { get; set; }
}
}
The result:

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html