WPF - Deselect DataGrid items - wpf

I have the following window:
When I click outside the data grid, there is still something selected, it just grays out when I change tabs and come back:
I wanted to deselect the items when clicked outside the datagrid, but this gives me a problem, since the Change button is outside, it deselects everything and there is nothing more to change and my Command changes the button state do disabled:
I did this with a simple TargetedTriggerAction, but I can't make it work in such a way that it only fires when clicked outside the datagrid AND outside the Button. I tried to make a GotFocus on the grid inside the tab, but it doesn't fires, it only fires in the tab item or the tab control and basically fires everywhere when tied to it.
My structure is: Window -> DockPanel -> TabControl -> TabItem -> Grid (Stack panel for the buttons) (Datagrid)
My TargetedTriggerAction:
public class TargetedTriggerActionDataGridUnselect : TargetedTriggerAction<DataGrid>
{
protected override void Invoke(object parameter)
{
this.Target.UnselectAll();
}
}

Related

Devexpress Popup menu with GridControl Popup menu

Where I Right-Click on GridColumn To Popup the GridColumn Menu
It Pops up with the main Popup menu
Your problem appears to be caused by PopupMenu being called all over regardless of the location of the mouse.
Use event "PopupMenuShowing" to display the user menu in GridControl. You can implement this event to decide whether to display pop-ups based on the location of your mouse.
private void gridview1_PopupMenuShowing(object sender, PoupMenuShowingEventArgs e)
{
// check in row and in cell
if(!e.HitInfo.InRow && !e.HitInfo.InRowCell)
return;
// showing main(user) popup menu
popupMenu1.ShowPopup(gridcontrol1.PointToScreen(new Point(e.Point.X, e.Point.Y)));
}

Click outside component to unfocus

By default, I need to focus on another component (a button or a textBox) to unfocus from the current one.
I want to just click outside.
So for example if I click on a textBox and write something, than click outside the TextBox, I shouldn't be able to type because the component is unfocused.
(I hope my explanation is clear, if not, please say so in the comments)
You can achieve this functionality by attaching to PreviewMouseDown event of a Window. Also in order to Lose Focus you need another focusable control (e.g. a text box named _dummyControl) to move focus to.
public MainWindow()
{
this.PreviewMouseDown += PreviewMouseDownEventHandler;
}
private void PreviewMouseDownEventHandler(object sender, MouseButtonEventArgs e)
{
//check conditions here...
_dummyControl.Focus();
}
more info:
PreviewMouseDown event occurs just before a MouseDown or click event occurs. By attaching to this event your code can tell when user clicks on the window and at that time you should move focus to a hidden control to simulate unfocus functionality.

GridViewColumnHeader.Click triggered by clicking ListView's scrollbar shaft & scroll arrows

I am using attached behaviours to add the ability to sort a ListView by clicking on a column's header. The behaviour adds the following handler to handle the user clicking on a GridViewColumnHeader:
listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
The handler looks something like this:
static void ColumnHeader_Click(object sender, RoutedEventArgs e)
{
var listView = sender as ListView;
var header = e.OriginalSource as GridViewColumnHeader;
var gridView = ((GridView)(listView.View));
...
}
I just noticed that if the ListView has a scroll bar, and I click on the scroll bar's 'shaft' or scroll arrows (but not thumb!):
(source: microsoft.com)
then the GridViewColumnHeader.ClickEvent is triggered, and my code fails because 'header' is now null. Obviously this isn't an expected behaviour, and now I have to make sure that the OriginalSource is a GridViewColumnHeader.
Why does this happen?
The problem is, GridViewColumnHeader.ClickEvent actually is ButtonBase.ClickEvent, which means that the handler you set will be triggered by a user click on any button (or derived: checkbox, datepicker, scrollbar button, etc.) in the ListView, and not only on a column header (which is derived from button).
See http://social.msdn.microsoft.com/Forums/en/wpf/thread/3595d153-4faa-4501-9c6f-2f074658e760
The only solution I've found is to check that header != null before doing anything else, to exit the handler when the button that triggered it (e.OriginalSource) was not a column header.
Hope this helps...
(PS: also check if header.Column != null, to avoid an error when the user clicks on the "last extra header", the empty header on the right of all the "regular" headers...)

Have TreeView update its SelectedItem when programmatically changed

In WPF I have a TreeView control where a particular item can be selected either by the user selecting the item directly in the tree view or by clicking on a screen control. The tree view is displaying a list of elements that are being displayed on a user defined form, basically a form designer application.
Here is my problem. When the user clicks on a screen control it calls a method that returns the TreeViewItem that represents the element. It then sets the IsSelected property to true for this element. It correctly changes the visual indicator in the TreeView and it raised the SelectedItemChanged event in the TreeView. This is all good.
However, it appears that somewhere behind the scenes the TreeView still thinks the previous item is selected. Here is why I have come to this conclusion. If I select ElementA by clicking on it in the TreeView is it correctly selected. If I then select ElementB by clicking on the screen control and programmatically setting the IsSelected property for the ElementB TreeViewItem it appears to have selected it correctly. Now if I select ElementA again by clicking on it in the TreeView it does nothing. The SelectedItemChanged event is not raised and the reverse selection box that indicates the selected item stays on ElementB. If I click on ElementB in the TreeView it also does not raise the SelectedItemChanged event, however it does not appear to update the internal flag since if I then click on ElementA on the TreeView it processes it correctly and raises the event.
The only workaround that I have found for this is in the SelectedItemChanged event handler to call the Focus method for the now selected TreeViewItem. If I do this I get the expected behaviour when I select screen controls and programmatically change the selected TreeViewItem.
This is not an acceptable solution though as it creates focus change flicker. When I select items on my form window the focus goes to the TreeView control and then back to the form, causing flicker and slight delay.
Anyone have any indeas.
Update
As requested here is some code. Here is my method of my Explorer window which is the manager of the TreeView in question.
public bool SelectItemByName(String controlName)
{
bool fReturn = false;
TreeViewItem tviToSelect = FindItemByName(_tviMaster, controlName);
if (tviToSelect != null && _tviSelectedItem != tviToSelect)
{
tviToSelect.IsSelected = true;
// Make sure the selected item is visible in the TreeView by expanding all of the parent nodes
ExpandAllParents(tviToSelect);
tviToSelect.BringIntoView();
fReturn = true;
}
return fReturn;
}
Every element has a unique identifier that I use as a cross reference between different areas of the interface. When you click a screen control it uses its identifier to find the cooresponding TreeViewItem in the TreeView. Then this code sets it as selected.
Then in my SelectedItemChanged event handler I had to include the following line.
_tviSelectedItem.Focus();
This fixes my initial issue but introducing the unwant screen flicker.
To recap, I select ElementA in the TreeView directly, then select one or more other elements in the form designer which in turn calls SelectItemByName to programatically set the selected item. All visual indicators show that this worked. In the TreeView the highlighted item changes to the new item that is selected. After selecting any number of elements through the form designer interface if you select ElementA by clicking on it directly in the TreeView it does nothing. It does not get highlighted and it does not fire the SelectedItemChanged event. If you inspect the SelectedItem and SelectedValue properties of the TreeView they all correctly coorespond to the item that was programmatically selected. However, the control somewhere appears to think that ElementA is still selected and doesn't recognize that the selection is changing.
I cannot believe that other people haven't run into this. It appears to be a significant flaw in the TreeView contol in WPF. Not sure if WinForms has the same issue or not.
Each TreeViewItem has an IsSelected property, and I suspect the old one isn't getting set to false. Try setting it to false whenever you set the new item to true.
var currentItem = treeView.ItemContainerGenerator
.ContainerFromItem(treeView.SelectedItem) as TreeViewItem;
currentItem.IsSelected = false;
If that doesn't work, try setting focus on the newly selected item at the same time as when you select it. Don't forget that WPF also has two focus scopes: Logical Focus and Keyboard Focus. You may need to set both.
treeViewItem.Focus(); // Sets Logical Focus
Keyboard.Focus(treeViewItem); // Sets Keyboard Focus

Control.Leave Triggered by MouseDown, not MouseUp

I have a C# .NET WinForm. In the form, I allow a user to add an item to a ListView by double-clicking in the ListView. This adds a TextBox control to the ListView and places the key cursor in the TextBox so a user can type.
I detect that a user is done with an item in a couple of ways (e.g. pressing Enter, Esc, Tab...), but also when they Leave (TextBox.Leave) the TextBox.
The problem is this set of steps:
User triggers TextBox.Leave by mousing down outside of the TextBox.
I add the new item to the ListView.
I select the the new item in the ListView.
Mouse up occurs and the new item that I just selected, loses focus and is unselected.
What I would like is for TextBox.Leave to be triggered by MouseUp, not MouseDown. How can I accomplish this?
Edit: Cody suggests using the ListView.LabelEdit property. Here are my results trying that:
listView_DoubleClick(...) {
listView.LabelEdit = true;
if(double clicked on existing listViewItem) {
listViewItem.BeginEdit(); //this works as expected
} else {
var newItem = listView.Items.Add("");
newItem.BeginEdit(); //this doesn't work, see below
}
}
The call to newItem.BeginEdit() only works when the user double clicks where the new item will show up. If they double click on any other blank area in the listview the new item is added, but it does not enter edit mode. What's going on here?
Pressing the mouse down on another control is causing that other control to request the focus and so the focus moving causes the TextBox.Leave event to occur. Preventing ever other possible control from requesting the focus is not a very viable option. But luckly you only need to prevent the ListView from using the MouseDown to shift focus. So you need to override the WndProc of your ListView and when the MouseDown windows message occurs and you are currently showing a TextBox you eat the message. In order words you do not allow the base class to process it.

Resources