0

I have a datagrid in WPF, I want to lose focus when you can run an event. I tried it with some events but to leave the cell or end a row is activated, I what I want is that when it comes completely datagrid activated, fails to find an event that fits.

DataGrid

Cristian
  • 191
  • 1
  • 2
  • 14
  • Your question is garbled. the "LostFocus" event is fired when you leave the datagrid control – BugFinder Jun 09 '16 at 13:11
  • Hello @BugFinder, I used the event indicas me but shooting 1. When I enter to edit the cell, 2. when I press the tab key to change cells. I just want to be active when you leave the datagrid. – Cristian Jun 09 '16 at 13:32
  • Im sorry but your english isnt making enough sense .. – BugFinder Jun 09 '16 at 13:34
  • @BugFinder Hi, I'm using the LostFocus event. but when I go to edit the cell the event runs. Also when I press the tab key, the event runs, I just want to run when I leave the datagrid. – Cristian Jun 09 '16 at 13:40
  • When you press `Tab` key `FocusManager` will pass focus to next control. Do you want to [disable Tab navigation](http://stackoverflow.com/q/4210659/1997232) or what? Try to describe what is that you call "leave datagrid"? – Sinatr Jun 09 '16 at 13:45
  • Hello @Sinatr, I mean the DataGrid control. when I press the tab key event "LostFocus" starts and I want to start when I leave the datagrid. I update the post and put a picture of the datagrid. – Cristian Jun 09 '16 at 14:02

1 Answers1

1

Try LostFocus event on the DataGrid and the DataGridCell, check the below code and keep breakpoints in the events DataGrid_LostFocus, OnCellLostFocus and OnCellGotFocus, click on a cell then click another cell and see that events are executed this way, OnCellLostFocus - - > OnCellGotFocus - - > DataGrid_LostFocus Maybe this can help you try it out...

UPDATED ANSWER:

The LostFocus event on the DataGrid will suffice, check the modified answer below

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="MyDataGrid"
                  LostFocus="DataGrid_LostFocus" 
                  ItemsSource="{Binding Persons}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="FirstName" Width="*"/>
                <DataGridTextColumn Header="LastName" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>

        <TextBox Grid.Column="1"/>
    </Grid>
</Window>

namespace WpfApplication1
{
    using System.Collections.Generic;
    using System.Windows;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Person> Persons { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Persons = new List<Person>
            {
                new Person { FirstName = "First", LastName = "Last" },
                new Person { FirstName = "First", LastName = "Last" }
            };
        }

        private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!MyDataGrid.IsKeyboardFocusWithin)
            {
                MessageBox.Show("DataGrid LostFocus called");
            }
        }
    }

    public class Person
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }
    }
}
Abdul Mateen Mohammed
  • 1,864
  • 1
  • 12
  • 21
  • Hi @abdul mateen, thanks for the help. I still do not succeed, the message `MessageBox.Show ("DataGrid LostFocus called");` It never is shown, even if I leave the grid. – Cristian Jun 09 '16 at 23:13
  • Sorry I couldn't set the flags correctly because the OnCellGotFocus is firing when I set focus on the textbox i.e. outside the DataGrid, I was thinking the way these events are executed might give you a hint, see my updated answer – Abdul Mateen Mohammed Jun 10 '16 at 07:11