Hello,
to reproduce this behavior, you have to, for example, open the sample xamGrid/Editing & Selection/Selection from the samples browser (I use v16.1) and click inside the white empty area at the bottom. This will activate the first cell / row and set the ActiveItem property. Even, if you set everything to "select rows only".
Can I disable this "feature"? So, that a click inside the background doesn't have any effect.
Regards
Stefan
Hello Stefan,
I have been looking through the source code of the XamGrid and this behavior appears to be expected. In the XamGrid's internal handler for "GotFocus," if the currently editing cell and the current active cell is null, the first cell of the first row of the first visible column gets activated. I am unsure why exactly this is, but it is very explicitly created in the GotFocus handler. It is worth noting though, that if you set everything to select rows only, that cells can still be activated. Activated in this case essentially just means focused, whereas when something is selected, it goes into a different state. For example, if you had multiple row selection available, only the most recently selected row would be the "active" one.
To disable this, I would recommend handling the PreviewMouseDown event on the XamGrid. Inside the handler for this event, you can place the following code:
var x = e.OriginalSource;CellsPanel cp = Utilities.GetAncestorFromType(x as DependencyObject, typeof(CellsPanel), false) as CellsPanel;
if (cp == null){ e.Handled = true;}
Essentially, what this will do is it will check the visual tree of the "actual-clicked" element (e.OriginalSource) for a CellsPanel, which is the element that makes up the presenter for a row in the grid, whether this be a data row, header row, filter row, etc. Needless to say, this will be null when you click on the blank area of the grid, and by handling this event, you can prevent the grid from activating that first cell as the internal handler for MouseDown will not fire.
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Thank you, it works and I've created this behavior for this:
public class XamGridBackgroundClickBehavior : Behavior<XamGrid> { protected override void OnAttached() { base.OnAttached();
AssociatedObject.PreviewMouseDown += AssociatedObject_PreviewMouseDown; }
private void AssociatedObject_PreviewMouseDown(object sender, MouseButtonEventArgs e) { var source = e.OriginalSource as DependencyObject;
var cp = Utilities.GetAncestorFromType(source, typeof(CellsPanel), false) as CellsPanel;
if (cp == null) { e.Handled = true; } }
protected override void OnDetaching() { base.OnDetaching();
AssociatedObject.PreviewMouseDown -= AssociatedObject_PreviewMouseDown; } }
But there is an other issue like that:If you have multiple row selection enabled and deselect the last selected one, there is again a single cell active and an active item. This makes no sense because I only want to select rows or one single row. Or should I better ignore the ActiveItem property and focus on the SelectedRowsCollection?
Thank you for your update. I am glad that you now have an acceptable solution and behavior on your end that hides the active cell behavior.
Please let me know if you have any other questions or concerns on this matter.
Thank you for your assistance.
Of course, this is only useful for read only scenarios.
I now have a acceptable solution with a behavior, that provides me the selected item and a style, that hides the active cell.
If you were to completely disable the active-cell functionality in the XamGrid, I would imagine that you would have a hard time getting a cell to go into edit mode, as a cell being in edit mode requires that cell to be active and focused.
Still, if you are looking to prevent cell activation completely, on thing you could do is handle the ActiveCellChanging event of the XamGrid, which is cancellable by setting e.Cancel = true, or handle the ActiveCellChanged event and set the ActiveCell property of the XamGrid back to null inside. This should allow you to prevent the activation of the cells in the grid.
If you would like to see a property added to the grid in the future to make this cell activation behavior optional, I would recommend that you suggest a new product idea for it at http://ideas.infragistics.com. This product ideas site will place you in direct communication with our product management teams who plan and prioritize upcoming features and development based on community and user feedback.
Hi,
I understand the way it works. And, of course, there will be an active item. But in my opinion, there must not be an active or focused cell, if I turn off every option of cell selection and cell click action. Maybe you could make this behavior optional in a future version to make this more consistent and easier to achieve.
In the XamGrid, there is almost always an ActiveItem as long as the grid is focused. Activated states work differently than selected states in the way that a cell or record can be active without being selected and vice versa. Essentially the "Active" item in the XamGrid will be the row or cell that is "focused" which will normally be the last one that you clicked. There can only ever be a single active item, as well.
In this case, if you are looking for data items, I would continue looking at the SelectedRowsCollection. Inside, you will find a full list of all of the Rows that are selected. Each of these Row objects have a Data property which will return the underlying selected data item. I would recommend that you use this instead of the ActiveItem property in the XamGrid if you are looking for a true, "selected" state of your data items.