SelectedRow of datagrid is not selected again after returning to same Page

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 19:03:30

问题


I have two different Pages called Select and Edit. Both of them shares same ViewModel named SalesAccountsViewModel.

I have a DataGrid in Select Page as follows:

Then I select it any Item from the DataGrid :

Then I click on edit button on selected row as shown below:

After that I am redirected to another Page as shown below:

When I click on save button:

I am again redirected to the Previous Page but can you see the first row? It is not Highlighted fully:

Here is Select Page that I use:

<Page x:Class="MiniAccountWPF.Views.Pages.Masters.SalesAccounts.Select"
      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:vm="clr-namespace:MiniAccountWPF.ViewModels.Masters"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Select" DataContext="{StaticResource salesAccountsViewModel}">

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Margin="0,10" HorizontalAlignment="Left" 
                Content="{Binding SelectedChildMenuItem.MenuItemName, Converter={StaticResource createButtonContentConverter}, Source={StaticResource mainWindowViewModel}}" />

        <DataGrid Grid.Row="1" ItemsSource="{Binding Ledgers}" SelectedValue="{Binding SelectedLedger}" 
                  IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Customer Name" Binding="{Binding LedgerName}" />
                <DataGridTextColumn Header="City" Binding="{Binding City}" />
                <DataGridTemplateColumn Header="Mobile Numbers">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock>
                                    <Run Text="{Binding MobileNo1, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=N}" />
                                    <Run Text="{Binding MobileNo1, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=S}" FontFamily="Consolas"/>
                                    <Run Text="   " FontFamily="Consolas"/>
                                    <Run Text="{Binding MobileNo2, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=N}" />
                                    <Run Text="{Binding MobileNo2, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=S}" FontFamily="Consolas"/>
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Opening Balance" Binding="{Binding OpeningBalance}" />
                <DataGridTemplateColumn Header="Edit">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Edit" Style="{StaticResource EditButton}" 
                                    Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Delete">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" Style="{StaticResource DeleteButton}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>

</Page>

Here is the Edit Page:

<Page x:Class="MiniAccountWPF.Views.Pages.Masters.SalesAccounts.Edit"
      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:vm="clr-namespace:MiniAccountWPF.ViewModels.Masters"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Edit" DataContext="{StaticResource salesAccountsViewModel}">

    <Grid DataContext="{Binding SelectedLedger}">
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="30" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="350" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="1" Grid.Column="1" Text="Name" Style="{StaticResource txtBlock}"/>
        <TextBlock Grid.Row="1" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Style="{StaticResource txtBlock}"/>
        <TextBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="3" Text="{Binding LedgerName}"/>

        <TextBlock Grid.Row="3" Grid.Column="1" Text="City" Style="{StaticResource txtBlock}"/>
        <TextBlock Grid.Row="3" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Style="{StaticResource txtBlock}"/>
        <TextBox Grid.Row="3" Grid.Column="2" Text="{Binding City}"/>

        <TextBlock Grid.Row="5" Grid.Column="1" Text="Mobile No." Style="{StaticResource txtBlock}"/>
        <TextBlock Grid.Row="5" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Style="{StaticResource txtBlock}"/>
        <TextBox Grid.Row="5" Grid.Column="2" Text="{Binding MobileNo1}"/>
        <TextBox Grid.Row="5" Grid.Column="4" Text="{Binding MobileNo2}"/>

        <TextBlock Grid.Row="7" Grid.Column="1" Text="Opening Balance    " Style="{StaticResource txtBlock}"/>
        <TextBlock Grid.Row="7" Grid.Column="1" Text=" : " HorizontalAlignment="Right" Style="{StaticResource txtBlock}"/>
        <TextBox Grid.Row="7" Grid.Column="2" Text="{Binding OpeningBalance}"/>

        <Grid Grid.Row="10" Grid.Column="4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="10" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="1" Content="Save" 
                    Command="{Binding DataContext.EditSaveCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/>
            <Button Grid.Column="3" Content="Cancel" HorizontalAlignment="Left" 
                    Command="{Binding DataContext.EditCancelCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"/>

        </Grid>

    </Grid>

</Page>

The ViewModel:

namespace MiniAccountWPF.ViewModels.Masters
{
    public class SalesAccountsViewModel: ViewModelBase, IModule
    {
        public SalesAccountsViewModel()
        {

            SessionViewModel.Instance.ModulesOpen.Add((IModule)this);

            using (MiniAccountDBEntities db = new MiniAccountDBEntities())
            {
                Ledgers = new ObservableCollection<Ledger>(db.Ledgers.Where(x => x.LedgerType.LedgerTypeName == "Customer"));
            }

            EditCommand = new RelayCommand(Edit_Click);
            EditSaveCommand = new RelayCommand(Edit_Save_Click);
            EditCancelCommand = new RelayCommand(Edit_Cancel_Click);
        }

        ~SalesAccountsViewModel()
        {
            SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
        }

        private ObservableCollection<Ledger> _ledgers;
        public ObservableCollection<Ledger> Ledgers
        {
            get
            {
                return _ledgers;
            }
            set
            {
                _ledgers = value;
                OnPropertyChanged("Ledgers");
            }
        }

        private Ledger _selectedLedger;
        public Ledger SelectedLedger
        {
            get
            {
                return _selectedLedger;
            }
            set
            {
                _selectedLedger = value;
                OnPropertyChanged("SelectedLedger");
            }
        }

        public ICommand EditCommand { get; set; }

        private void Edit_Click(object obj)
        {
            var mainWindowVM = SessionViewModel.GetModuleInstance("MainWindow", "MiniAccountWPF.ViewModels.MainWindowViewModel");
            ((MainWindowViewModel)mainWindowVM).InnerSourcePage = ((MainWindowViewModel)mainWindowVM).SelectedChildMenuItem.EditFrameNavigationURL;
        }

        public ICommand EditSaveCommand { get; set; }

        private void Edit_Save_Click(object obj)
        {
            using (MiniAccountDBEntities db = new MiniAccountDBEntities())
            {
                Ledger ledger = db.Ledgers.Single(x => x.LedgerID == SelectedLedger.LedgerID);
                ledger.LedgerName = SelectedLedger.LedgerName;
                ledger.City = SelectedLedger.City;
                ledger.MobileNo1 = SelectedLedger.MobileNo1;
                ledger.MobileNo2 = SelectedLedger.MobileNo2;
                ledger.OpeningBalance = SelectedLedger.OpeningBalance;
                db.SaveChanges();

                var mainWindowVM = SessionViewModel.GetModuleInstance("MainWindow", "MiniAccountWPF.ViewModels.MainWindowViewModel");
                ((MainWindowViewModel)mainWindowVM).InnerSourcePage = ((MainWindowViewModel)mainWindowVM).SelectedChildMenuItem.SelectFrameNavigationURL;
            }
        }

        public ICommand EditCancelCommand { get; set; }

        private void Edit_Cancel_Click(object obj)
        {
                var mainWindowVM = SessionViewModel.GetModuleInstance("MainWindow", "MiniAccountWPF.ViewModels.MainWindowViewModel");
                ((MainWindowViewModel)mainWindowVM).InnerSourcePage = ((MainWindowViewModel)mainWindowVM).SelectedChildMenuItem.SelectFrameNavigationURL;
        }

        public string ModuleFriendlyName
        {
            get { return "SalesAccountsViewModel"; }
        }

        public string ModuleName
        {
            get { return "SalesAccounts"; }
        }
    }
}

Styles applied to DataGrid in ResourceDictionary:

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="GridLinesVisibility" Value="Vertical" />
    <Setter Property="RowHeaderWidth" Value="0" />
</Style>

<Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="Background" Value="{StaticResource BrushHeaderBackground}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Padding" Value="10" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="FontWeight" Value="SemiBold" />
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="BorderBrush" Value="White" />
    <Setter Property="BorderThickness" Value="1,0" />

    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="CanUserSort" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="{StaticResource BrushOrangeSelector}" />
            </MultiTrigger.Setters>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsPressed" Value="True" />
                <Condition Property="CanUserSort" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="{StaticResource BrushBlueSelector}" />
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type DataGridCell}">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="Background" Value="Gray" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Padding" Value="10" />
    <Setter Property="FontSize" Value="16" />

    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushBlueSelector}" />
        </Trigger>
    </Style.Triggers>

</Style>

<Style TargetType='{x:Type DataGridRow}'>
    <Setter Property="Background" Value="Gray" />
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushBlueSelector}" />
        </Trigger>
    </Style.Triggers>
</Style>

回答1:


the issue was view model, which was getting created with every view, leading to multiple copies of person collection

so I did place a property for VM in ServiceLocator for single instance and initialized the same

public ListViewModel ListViewModel {get; set;}

then I replaced the declaration of view model from the list view to point to this property

DataContext="{Binding ListViewModel, Source={x:Static vm:ServiceLocator.Instance}}"

this solves the problem of multiple instance of the collection but again datagrid has some problem redoing the selection so I added a style with datatrigger to reapply the selection

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="IsSelected"
                         Value="true">
                    <Setter Property="Background"
                            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    <Setter Property="Foreground"
                            Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>

this trigger will ensure the row looks selected even if the page is reloaded.

here is a working sample


EDIT

here is a solution for the issue after merging Metro.xaml

remove DataGrid.RowStyle from the DataGrid in ListView and replace the mentioned styles in Metro.xaml with these

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="FontSize" Value="16" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Black" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType='{x:Type DataGridRow}'>
    <Setter Property="TextElement.Foreground" Value="White" />
    <Setter Property="Background" Value="Gray" />
    <Setter Property="Height" Value="50" />
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#FFE1AF4D" />
            <Setter Property="TextElement.Foreground" Value="Black" />
        </Trigger>
    </Style.Triggers>
</Style>



回答2:


It might have lost focus. I have seen similar problems where WPF controls are partially rendered.

Try putting the focus back to the target grid control.



来源:https://stackoverflow.com/questions/25502250/selectedrow-of-datagrid-is-not-selected-again-after-returning-to-same-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!