wpfxamlmvvminfragisticsxamdatagrid

Scroll to Selected Row in XamDataGrid


I have an Infragistics 15.1 WPF XamDataGrid that refreshes when the user saves data elsewhere on the form. I have figured out how to programmatically select the row that was selected before saving. My problem is that if that row is not one of the top rows on the grid the user has to scroll back down to the highlighted row. Is there a way to have the grid scroll to the selected row?

XAML Code

 <inf:XamDataGrid GroupByAreaLocation="None"                               
                             SelectedDataItem="{Binding SelectedPayItem, Mode=TwoWay}"
                             ActiveDataItem="{Binding SelectedPayItem, Mode=OneWay}"
                             DataSource="{Binding SelectedProject.ContractProjectPayItems}" ScrollViewer.VerticalScrollBarVisibility="Auto"

                             Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" Margin="2.8,3.4,3,2.8">

View Model Code

  private void SetSelectedPayItem()
    {
        if (SelectedProject != null)
        {
            if (SelectedProject.ContractProjectPayItems.Count() > 0)
            {
                if (SelectedProject.SelectedPayItemLineNbr == -1)
                {
                      SelectedPayItem = SelectedProject.ContractProjectPayItems.First(); 
                }
                else
                {
                    if (strLineItemNbr != null)
                    {
                       SelectedPayItem = SelectedProject.ContractProjectPayItems.FirstOrDefault(CPPI => CPPI.LineItemNbr == strLineItemNbr);
                    }
                    else
                    { SelectedPayItem = SelectedProject.ContractProjectPayItems[SelectedProject.SelectedPayItemLineNbr]; }
                }
            }
            else
            {
                SelectedPayItem = null;
            }
        }
    }

    string strLineItemNbr;
    private ContractProjectPayItemModel _selectedPayItem;
    public ContractProjectPayItemModel SelectedPayItem
    {
        get { return _selectedPayItem; }
        set
        {
            _selectedPayItem = value;
            if (_selectedPayItem != null)
            {
                SelectedProject.SelectedPayItemLineNbr = SelectedProject.ContractProjectPayItems.IndexOf(_selectedPayItem);
                if (_selectedPayItem.ItemInstallations == null)
                {
                    var oItemInstallation = new clsItemInstallation();
                    _selectedPayItem.ItemInstallations = oItemInstallation.GetItemInstallsByProjectPayItem(_selectedPayItem.ProjectGuid, _selectedPayItem.PayItemGuid);

                    foreach (var itemInstallation in _selectedPayItem.ItemInstallations)
                    {
                        itemInstallation.PropertyChanged += ItemInstallationsPropertyChanged;
                        itemInstallation.AcceptChanges();
                    }
                    _selectedPayItem.AcceptChanges();
                    foreach (var ii in SelectedProject.ContractProjectPayItems)
                    {
                        if (ii.ItemInstallations != null)
                        {
                            foreach (var i2 in ii.ItemInstallations)
                            {
                                i2.AcceptChanges();
                            }
                        }
                    }
                    RaisePropertyChanged("TotalInstallQty");
                }
                _itemInstallViewModel.SelectedProjectLineItem = SelectedPayItem;
                strLineItemNbr = SelectedPayItem.LineItemNbr;
            }
            RaisePropertyChanged("SelectedPayItem");
            RaisePropertyChanged("IsBitumen");

            //----------------------------------
            // Populate the User Control tabs.
            //----------------------------------
            PopulateUserControls();

            SetSelectedItemInstallation();

            RaisePropertyChanged("TotalInstallQty");
        }
    }

Solution

  • I was able to get this to behave the way I wanted by moving the SelectedDataItem inside of the DataSource property.

    <inf:XamDataGrid GroupByAreaLocation="None"             
                             DataSource="{Binding SelectedProject.ContractProjectPayItems}" 
                             ScrollViewer.VerticalScrollBarVisibility="Auto" 
                             SelectedDataItem="{Binding SelectedPayItem, Mode=TwoWay}" 
                             ActiveDataItem="{Binding SelectedPayItem, Mode=TwoWay}"
                             Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" Margin="2.8,3.4,3,2.8">
            </inf:XamDataGrid>