wpf关闭弹出窗口后,Datagrid失去焦点

wpf关闭弹出窗口后,Datagrid失去焦点

问题描述:

嗨!



我是PRISM和WPF的新手,对我的英语也很抱歉。

我的问题如下:

我有一个数据网格,当用户想删除行时,我通过确认弹出窗口(棱镜)通知他/她。如果他/她接受,将删除所选行,但如果他/她取消该过程,则在关闭此窗口后,数据网格不会恢复他的焦点。

所选记录仅灰色而不是蓝色,就像在默认情况下一样。



这是我的ViewModel:



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:

/// <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

                    }
                });
        }
    }         
}





你能帮帮我吗,我怎样才能解决这个问题?

我正在使用MVVM模式,如果解决方案是在xaml或ViewModel中,那将会很棒。



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.

设置关注PreviewKeyDown-Event中的datagrid可能有所帮助。

检查以下代码 -

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





参考: http://*.com/a/15840916/1006297 [ ^ ]



希望,它有帮助:)



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

Hope, it helps :)