Hello I am pretty new to using Infragistics and have been tasked to take over code that has been written by a previous developer.Based on my observations, this developer implemented a custom Infragistics XamDataGrid that allows one to bind to the SelectedItems property as the SelectedItems property does not allow binding by default according to him,.
Preview of code:
<CustomControls:BSIXamDataGrid x:Uid="xamDataGridCam" x:Name="xamDataGridCam" DataSource="{Binding QueryItems}" BindableSelectedItems="{Binding SelectedItems, Mode=TwoWay}" GroupByAreaLocation="None" GotKeyboardFocus="xamDataGridCam_GotKeyboardFocus" AutoFit="True" Grid.Row="4" VerticalContentAlignment="Stretch" ScrollingMode="Immediate" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" VerticalAlignment="Stretch" RecordsInViewChanged="xamDataGridCam_RecordsInViewChanged" RecordFilterDropDownPopulating="xamDataGridCam_RecordFilterDropDownPopulating" AssigningFieldLayoutToItem="xamDataGridCam_AssigningFieldLayoutToItem">
There seems to be a bug where if I try to navigate through the rows in a table using the arrow keys, it will only go to the next row then go back to the previous like so:
Could anyone please help me understand what's possibly going on? Thanks in advance!
Hello Shawn,
Thank you for contacting Infragistics. What happens when you remove BindableSelectedItems="{Binding Select .."? Does this fix the issue?
Please note that we added a way to bind to selected items called SelectedDataItems, added in 2014 in our Volume 2 package. I highly recommended that you migrate towards implementing by removing the custom implementation if it is feasible. Do you know when and if the custom implementation was only for this very requirement?
Let me know if you have any questions.
Hi Michael,
Removing that does not solve the issue and only adds the problem of my application not recognizing that a row has been selected. As for migrating to the newer package, I do not think that is feasible at this moment in time. From my knowledge this custom class has been implemented a couple of months ago and is shown below:
using Infragistics.Windows.DataPresenter; using Infragistics.Windows.DataPresenter.Events; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace Vdi.VideoClient.SoftClient.CustomControls { /// <summary> /// The BSIXamDataGrid is a custom Infragistics XamDataGrid that allows one to bind to the SelectedItems property /// By default, the SelectedItems property does not allow binding. /// This class implements a BindableSelectedItems property which can be bound to a property in your ViewModel then manipulated as required /// from the ViewModel. /// The result is a XamDataGrid that appropriately displays highlights for when a record is selected. /// </summary> public class BSIXamDataGrid : XamDataGrid { public static readonly DependencyProperty BindableSelectedItemsProperty = DependencyProperty.Register("BindableSelectedItems", typeof(IEnumerable), typeof(BSIXamDataGrid)); public IEnumerable BindableSelectedItems { get { return (IEnumerable)GetValue(BindableSelectedItemsProperty); } set { SetValue(BindableSelectedItemsProperty, value); } } private void BindableSelectedItemsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { this.SelectedItemsChanged -= new EventHandler<SelectedItemsChangedEventArgs>(BSIXamDataGrid_SelectedItemsChanged); this.ActiveRecord = null; if (!this.IsMouseOver) { this.SelectedItems.Records.Clear(); } if (args.NewValue != null) { foreach (var item in args.NewValue as IEnumerable) { this.SelectedItems.Records.Add(this.GetRecordFromDataItem(item, false)); } } this.SelectedItemsChanged += new EventHandler<SelectedItemsChangedEventArgs>(BSIXamDataGrid_SelectedItemsChanged); } public BSIXamDataGrid() : base() { this.SelectedItemsChanged += new EventHandler<SelectedItemsChangedEventArgs>(BSIXamDataGrid_SelectedItemsChanged); BindableSelectedItemsProperty.OverrideMetadata(typeof(BSIXamDataGrid), new FrameworkPropertyMetadata(BindableSelectedItemsPropertyChanged)); } void BSIXamDataGrid_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e) { List<object> selectedItems = new List<object>(); foreach (object selectedItem in base.SelectedItems) { if (selectedItem is Cell) selectedItems.Add(((selectedItem as Cell).Record as DataRecord).DataItem); else if (selectedItem is DataRecord) selectedItems.Add((selectedItem as DataRecord).DataItem); } BindableSelectedItems = selectedItems as IEnumerable; } } }
Also I feel like this needs to be included as well:
<CustomControls:BSIXamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings x:Uid="igDP:FieldLayoutSettings_1" AllowRecordFixing="Top" HeaderPlacement="OnTopOnly" AllowDelete="False" RecordSelectorLocation="None" SelectionTypeRecord="ExtendedAutoDrag" AllowFieldMoving="WithinLogicalRow" HighlightAlternateRecords="False" AutoGenerateFields="False"/> </CustomControls:BSIXamDataGrid.FieldLayoutSettings>
The version of Infragistics my application is licensed to use is:
Assembly InfragisticsWPF4.DataPresenter.v12.1, Version=12.1.20121.2286
I'm told by management that upgrading to a newer version isn't financially feasible at the moment so I would like to figure out a work-around for this issue.
I strongly encourage you to review the following blog that contains a sample ( at the bottom of the page) demonstrating a working scenario of a derived XamDataGrid with a custom "selecteditems" dependency property. The blog was written before 14.2 when SelectedDataItems wasn't available. If the issue still occurs with the following implementation let me know.
eg.
public class MyXamDataGrid : XamDataGrid { public static readonly DependencyProperty NewXDGSelectedItemsProperty = DependencyProperty.Register("NewXDGSelectedItems", typeof(IEnumerable), typeof(MyXamDataGrid)); public IEnumerable NewXDGSelectedItems { get { return (IEnumerable)GetValue(NewXDGSelectedItemsProperty); } set { SetValue(NewXDGSelectedItemsProperty, value); } } private void NewXDGSelectedItemsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { this.SelectedItemsChanged -= new EventHandler<SelectedItemsChangedEventArgs>(MyXamDataGrid_SelectedItemsChanged); this.ActiveRecord = null; if (!this.IsMouseOver) { this.SelectedItems.Records.Clear(); } if (args.NewValue != null) { foreach (var item in args.NewValue as IEnumerable) { this.SelectedItems.Records.Add(this.GetRecordFromDataItem(item, false)); //(item as TestItem).IsSelected = true; } } this.SelectedItemsChanged += new EventHandler<SelectedItemsChangedEventArgs>(MyXamDataGrid_SelectedItemsChanged); } public MyXamDataGrid() :base() { this.SelectedItemsChanged += new EventHandler<SelectedItemsChangedEventArgs>(MyXamDataGrid_SelectedItemsChanged); NewXDGSelectedItemsProperty.OverrideMetadata(typeof(MyXamDataGrid), new FrameworkPropertyMetadata(NewXDGSelectedItemsPropertyChanged)); } void MyXamDataGrid_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e) { List<object> selectedItems = new List<object>(); foreach (object selectedItem in base.SelectedItems) { if (selectedItem is Cell) selectedItems.Add(((selectedItem as Cell).Record as DataRecord).DataItem); else if (selectedItem is DataRecord) selectedItems.Add((selectedItem as DataRecord).DataItem); } NewXDGSelectedItems = selectedItems as IEnumerable; } }
I reproduced the behavior, it's definitely not anything in XAML that's causing it. The issue stems from updating NewXDGSelectedItems when grid's SelectedItemsChanged event is fired. I haven't been able to work around this. But you might be better off using a Behavior with your custom implementation. This was discussed here
I'm in the process of adding said Behavior in conjunction with my custom implementation but I'm having a bit of trouble. I believe this method fires as soon as the application launches when it really should run once the window containing the XamDataGrid is launched/opened:
protected override void OnAttached() { base.OnAttached(); Grid.SelectedItemsChanged += new System.EventHandler<Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs>(Grid_SelectedItemsChanged); SelectedItems.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedItems_CollectionChanged); }
The error is that the SelectedItems object appears to be null as it hasn't even been created in the first place:
If you are implementing the Behavior approach, then I don't recommend also using a custom implementation of XamDataGrid. Do you have any requirements other than handling SelectedItems?
I attached a more refined sample demonstrating the Behavior working with a regular XamDataGrid. It's more efficient to use a Behavior than a custom implementation of XamDataGrid since you are not using SelectedDataItems in our newer versions.
Please review the sample attached below.
Grid_Binding_SelectedItems.zip
I'd like to add that the detach code wasn't working because I was passing the behavior instead of the grid.
Hi Michael,I'm glad to say that I have finally got it working with all your help!Here's what I did:
I am now able to cycle through the table using my arrow keys and the selected rows are properly assigned accordingly. I will do some further testing but I just wanted to say thank you for all the help you've provided!
Thank you very much for your patience, I am glad I was able to assist you. Please let us know if you have any additional questions.
Have a great day!