Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm new in PRISM and WPF and sorry for my English too.
My problem is the following:
I have a datagrid, and when the user would like to delete rows, I notify him/her with a Confirmation Popup window (prism). If he/she accept, the selected rows will be deleted, but if he/she cancel the process, after closing this window, the datagrid doesn't get back his focus.
The selected record is only gray and not blue, like in default situation.

This is my ViewModel:

C#
/// <summary>
/// DataManipulationViewModel 
/// </summary>
public class DataManipulationViewModel : BindableBase
{
    private ObservableCollection<PriceCalcDS.BUTORLAPRow> collectionButorlap;
    private ObservableCollection<PriceCalcDS.BUTORLAPRow> currentButorlap;
    private ICollectionView colViewButorlap;
    private readonly ICommand deleteCommandButorlap;
    private Confirmation confirmation;

    /// <summary>
    /// Confirmation to deleting records
    /// </summary>
    public InteractionRequest<IConfirmation> ConfirmationRequestDeletingRecords { get; private set; }

    /// <summary>
    /// Current BUTORLAP
    /// </summary>
    public ObservableCollection<PriceCalcDS.BUTORLAPRow> CurrentButorlap
    {
        get { return currentButorlap; }
        set
        {
            this.SetProperty(ref this.currentButorlap, value);
        }
    }

    /// <summary>
    /// The rows of the table BUTORLAP
    /// </summary>
    public ICollectionView ColViewButorlap
    {
        get { return colViewButorlap; }
        set
        {
            this.SetProperty(ref this.colViewButorlap, value);
        }
    }

    /// <summary>
    /// Delete command
    /// </summary>
    public ICommand DeleteCommandButorlap
    {
        get { return deleteCommandButorlap; }
    }

    /// <summary>
    /// Constructor
    /// </summary>
    public DataManipulationViewModel()
    {
        DataBase.FillDataSet();
        this.collectionButorlap = new ObservableCollection<PriceCalcDS.BUTORLAPRow>();
        this.currentButorlap = new ObservableCollection<PriceCalcDS.BUTORLAPRow>();


        FillDtButorlap();

        this.colViewButorlap = new ListCollectionView(collectionButorlap);


        this.colViewButorlap.CurrentChanged += SelectedButorlapChanged;


        this.deleteCommandButorlap = new DelegateCommand<object>(this.DeleteRecordButorlap);


        this.ConfirmationRequestDeletingRecords = new InteractionRequest<IConfirmation>();
        this.confirmation = new Confirmation();
    }

    /// <summary>
    /// SelectedChanged event
    /// </summary>
    private void SelectedButorlapChanged(object sender, EventArgs e)
    {
        // Deleteing previous values from currentButorlap
        this.currentButorlap.Clear();

        if (colViewButorlap.CurrentItem != null)
        {
            this.currentButorlap.Add(colViewButorlap.CurrentItem as PriceCalcDS.BUTORLAPRow);
        }
    }

    /// <summary>
    /// Fill collectionButorlap with the rows of the table BUTORLAP
    /// </summary>
    private void FillDtButorlap()
    {
        foreach (var item in StaticDataSet.priceCalcDs.BUTORLAP.Rows)
        {
            collectionButorlap.Add(item as PriceCalcDS.BUTORLAPRow);
        }
    }

    /// <summary>
    /// Deleting rows of the table BUTORLAP
    /// </summary>
    /// <param name="parameter">Deleting rows (selected in DataGrid)</param>
    private void DeleteRecordButorlap(object parameter)
    {
        IList selection = (IList)parameter;
        List<object> deletingRows = new List<object>();

        if (selection.Count != 0)
        {
            // Save deleting rows
            foreach (var selectionValue in selection)
            {
                deletingRows.Add(selectionValue);
            }

            // Notica init
            this.confirmation.Title = "Figyelmeztetés";

            if (deletingRows.Count == 1)
            {
                this.confirmation.Content = "Biztosan törli a kijelölt rekordot?";
            }
            else
            {

                this.confirmation.Content = "Biztosan törli a kijelölt rekordokat?";
            }

            // Notify user
            this.ConfirmationRequestDeletingRecords.Raise(
                this.confirmation,
                c =>
                {
                    if (c.Confirmed)
                    {
                        // If user accpted request, the rows will be deleted
                        foreach (var deletingRow in deletingRows)
                        {
                            collectionButorlap.Remove(deletingRow as PriceCalcDS.BUTORLAPRow);
                        }
                    }
                    else
                    {
                        //If user cancelled request

                    }
                });
        }
    }         
}


Can you help me please, how can I solve this problem?
I'm using MVVM pattern, and would be great, if the solution would be in xaml, or ViewModel.
Posted
Updated 8-Nov-15 22:18pm
v7

1 solution

Setting focus on datagrid in the PreviewKeyDown-Event may help.
Check the following code-
C#
private void YourDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
       var grid = (DataGrid)sender;
       FocusManager.SetFocusedElement(Window.GetWindow(grid), grid);
    }
}


Reference: http://stackoverflow.com/a/15840916/1006297[^]

Hope, it helps :)
 
Share this answer
 
Comments
Member 11844200 8-Nov-15 15:58pm    
Thank you, but I forgot, that I'm using MVVM pattern, and it would be great, if the solution would be in xaml or Viewmodel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900