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?