0
votes

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.

After cancel popup. IT'S BAD! Image1

It must be so! Image2

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

                    }
                });
        }
    }         
}

And this is my XAML for this:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:prism="http://www.codeplex.com/prism"
         xmlns:support="clr-namespace:Support"
         xmlns:PriceCalcDSTableAdapters="clr-namespace:PriceCalculator.Common.Dataset.PriceCalcDSTableAdapters;assembly=PriceCalculator.Common" x:Name="ucDataManipulation" x:Class="PriceCalculator.Modules.DataManipulation.Views.DataManipulationView" 
         mc:Ignorable="d" d:DesignWidth="1365" d:DesignHeight="750" Height="Auto" Width="Auto">
<UserControl.Resources>
    <Color x:Key="TitleBgColor">#FF183B3F</Color>
    <Color x:Key="TitleColor">#FF07333A</Color>
    <Color x:Key="ButtonColor">#FF3E4949</Color>
</UserControl.Resources>
<UserControl.Background>
    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
        <GradientStop Color="#FF022F36"/>
        <GradientStop Offset="0.994" Color="{DynamicResource ViewBaseColor}"/>
    </LinearGradientBrush>
</UserControl.Background>
<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequestDeletingRecords, Mode=OneWay}">
        <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
<Grid x:Name="gridDataManipulation" Cursor="Arrow" Focusable="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="8*"/>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="8*"/>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="165*"/>
        <ColumnDefinition Width="100"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="20"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition/>
        <RowDefinition Height="16"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="23"/>
        <RowDefinition Height="12"/>
    </Grid.RowDefinitions>
    <Border x:Name="brdFrame" BorderThickness="2" Padding="0" Margin="0,10,0,0" Grid.RowSpan="5" Grid.Column="1" Grid.ColumnSpan="9">
        <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource FrameColor}"/>
        </Border.BorderBrush>
    </Border>
    <TextBlock x:Name="txbTitle" TextWrapping="Wrap" Text="ADATMANIPULÁCIÓ" Margin="0,1,0,0" FontSize="16" FontFamily="Microsoft Sans Serif" FontWeight="Bold" TextAlignment="Center" Foreground="White" Height="20" Width="160" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="2" Grid.ColumnSpan="3">
        <TextBlock.Background>
            <SolidColorBrush Color="{DynamicResource TitleColor}"/>
        </TextBlock.Background>
    </TextBlock>
    <TabControl x:Name="tcDataManipulation" TabStripPlacement="Top" Margin="0,15,0,0" Grid.Row="1" Grid.Column="2" BorderBrush="White" Grid.ColumnSpan="7">
        <TabControl.Background>
            <SolidColorBrush Color="{DynamicResource FrameColor}"/>
        </TabControl.Background>
        <TabItem Header="BÚTORLAP" Margin="0" Padding="10,2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" FontSize="13.333" BorderThickness="2">
            <TabItem.BorderBrush>
                <SolidColorBrush Color="{DynamicResource ButtonColor}"/>
            </TabItem.BorderBrush>
            <TabItem.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFF0F0F0" Offset="0"/>
                    <GradientStop Color="{DynamicResource FrameColor}" Offset="1"/>
                </LinearGradientBrush>
            </TabItem.Background>
            <Grid x:Name="gridButorlap" Margin="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="35*"/>
                    <ColumnDefinition Width="57"/>
                    <ColumnDefinition Width="66"/>
                    <ColumnDefinition Width="129"/>
                    <ColumnDefinition Width="80*"/>
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="195"/>
                    <ColumnDefinition Width="30*"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="501*"/>
                    <ColumnDefinition Width="36*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="16*"/>
                    <RowDefinition Height="397*"/>
                    <RowDefinition Height="53*"/>
                    <RowDefinition Height="27"/>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="36"/>
                    <RowDefinition Height="16*"/>
                </Grid.RowDefinitions>
                <DataGrid x:Name="dgButorlap" ItemsSource="{Binding Path=ColViewButorlap, Mode=TwoWay}" EnableRowVirtualization="True" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" AutoGenerateColumns="False" Margin="5,0,5,10" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="9">
                    <DataGrid.RowStyle>
                        <Style TargetType="{x:Type DataGridRow}">
                            <Setter Property="support:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/>
                        </Style>
                    </DataGrid.RowStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="NÉV" Binding="{Binding Path=BL_NEV}"/>
                        <DataGridTextColumn Header="EGYSÉG" Binding="{Binding Path=BL_EGYSEG}"/>
                        <DataGridTextColumn Header="ÁR" Binding="{Binding Path=BL_AR}"/>
                        <DataGridTextColumn Header="HOSSZÚSÁG" Binding="{Binding Path=BL_HOSSZ}"/>
                        <DataGridTextColumn Header="SZÉLESSÉG" Binding="{Binding Path=BL_SZEL}"/>
                        <DataGridTextColumn Header="KIHOZATAL" Binding="{Binding Path=BL_KIHOZATAL}"/>
                    </DataGrid.Columns>
                </DataGrid>
                <Label Content="Felvitel/Módosítás" Margin="5,1,9,5" Foreground="White" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="3" Padding="0" Grid.Column="1" Grid.ColumnSpan="2"/>
                <DataGrid x:Name="dgBlFelvitelModositas" ItemsSource="{Binding Path=CurrentButorlap}" CanUserAddRows="False" AutoGenerateColumns="False" Margin="5,3,5,0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="4" Grid.Column="1" Grid.RowSpan="3" Grid.ColumnSpan="9">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="NÉV" Binding="{Binding Path=BL_NEV}"/>
                        <DataGridTextColumn Header="EGYSÉG" Binding="{Binding Path=BL_EGYSEG}"/>
                        <DataGridTextColumn Header="ÁR" Binding="{Binding Path=BL_AR}"/>
                        <DataGridTextColumn Header="HOSSZÚSÁG" Binding="{Binding Path=BL_HOSSZ}"/>
                        <DataGridTextColumn Header="SZÉLESSÉG" Binding="{Binding Path=BL_SZEL}"/>
                        <DataGridTextColumn Header="KIHOZATAL" Binding="{Binding Path=BL_KIHOZATAL}"/>
                    </DataGrid.Columns>
                </DataGrid>             
                <Button x:Name="btnBLNewRecord" Content="ÚJ FELVITELE" Style="{DynamicResource btnStyle}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Foreground="White" Grid.Column="1" Grid.ColumnSpan="2" Margin="5,16,18,0"/>
                <Button x:Name="btnBLModifyRecord" Content="MÓDOSÍTÁS" Style="{DynamicResource btnStyle}" Command="{Binding ModifyCommand}" CommandParameter="{Binding ElementName=dgBlFelvitelModositas, Path=SelectedItems}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Grid.Column="3" Foreground="White" Margin="0,16,29,0"/>
                <Button x:Name="btnBLDeleteRecord" Content="TÖRLÉS" Style="{DynamicResource btnStyle}" Command="{Binding DeleteCommandButorlap}" CommandParameter="{Binding ElementName=dgButorlap, Path=SelectedItems}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Grid.Column="4" Foreground="White" Grid.ColumnSpan="2" Margin="0,16,50,0"/>
            </Grid>
        </TabItem>

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.

1

1 Answers

0
votes

You should probably give your element the focus back (since you're dialog stole it away :) ).

all you need to do , is when he cancels, get your object, and give him the focus back:

Keyboard.Focus(object_you_want_focused);

You can find more on MSDN (if it says "unavailable", change the version to .Net 4, and you'll see it.... )