How to get the clicked event of a wpf data grid? - c#

I have a DataGrid with programmaticaly added rows (items) by adding an ItemSource.
I do not want the user to edit the cells so I set IsReadOnly = true;.
But I want them to click on cells and output which cell was clicked.
I was searching for a Clicked event and was surprised there is no such event for a DataGrid. I want a behaviour like buttons where it doesn't matter if it's clicked by the mouse, space bar, enter or whatever else the system accepts as a "Click".
So how can I achieve that?

Whatever is in the cell template (TextBlock, for instance) could be styled to handle the PreviewMouseUp event and pull the information you want from the control or bound object. The click event is specific to Button, TextBox and other controls that deal with mouse capture. Here is a link that discusses the dilemma you are facing: http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html. He provides examples on how to accomplish the task at hand.

Related

c# datagridview in tab control not drawing changes after first switch to tab

The setup:
I have a tab control and a datagridview (under the tabpageindex==2).
the datagridview is bound to a datatable.
In the tab control's tab_Selected event, if tabpageindex==2, I change some colors in some cells in the datagridview.
The problem:
The first time I select the tab with the datagridview in the application, the cells do not change color (i.e. the bound data is displayed, but my changing of colors does not work).
If I then click into another tab and back to this tab, then the colors appear.
Question:
Any idea why this is?
Should I be doing this differently, i.e. not in the tab_selected event?
(what I basically need is that certain cells have certain formats/colors depending on the data in the cells. I also e.g. call this drawing of cells function after the datagridview is sorted and there it works. just the first tab_selected event does not color...)
thanks for any help,
imran
Try doing the following:
After you update the cell, call InvalidateCell() on the DataGridView.
If that doesn't work, call Invalidate() on the DataGridView.
If that doesn't work, call Invalidate() on the TabControl.
It seems there is a bug with TabControl that when it has more than one tab, cellstyles that are created with code are only applied to the first tab's DataGridView, so you can move your DataGridView to the first tab or you can use the TabControl's SelectedIndexChanged event and put your styling code in this event .

WPF: How do I handle mouse click on a Listview inside of a Button?

I have a listview for displaying a table inside of a button. I have a click event assigned to the button, but when the user clicks on the list view, a row is selected on the list view and the mouse click is never bubbled up to the button.
I'm stuck at this point, and need a way to solve this. All the examples I've seen online are for placing a button inside the listview. How do I make this work?
Set the IsHitTestVisible property to false on your ListView. Since it is a control that normally processes click events, doing this will have it ignore them.
If you don't want the items to be selectable in the first place, you could make the List View unable to receive Focus. Just set the Focusable property to false.
With the List View unable to receive focus, then I would expect the mouse event to bubble up to the button.

Getting value of Combobox on Hover c#

I'm trying to make an application where I have this combobox that automatically opens and closes on mouse enter and mouse leave events respectively and then gets the value of the combobox before i leave it.
I already have the dropdown behavior as given by someone who posted it online. But now I need to get the value of my combobox item without clicking the mouse. I've tried selectedvalue or selecteditem but all seems to require mouse clicking. Is there any way of getting the value of the combobox where the mouse points to before I leave/close the combobox?
You can use labels for the combobox items.
Add 'MouseMove' event and in the event take the label text.
Here is an example how to implement very closely connected behavior: ComboBox MouseLeave MouseEnter.

C# PropertyGrid DoubleClick event capture

For a project I am working on, I have some values in a PropertyGrid that are many lines long. I want the user to be able to double-click a value in the grid, which will bring up my custom UI window. I already have it set up so that the UI works when the [...] button is clicked, but I also need it to show up directly if any grid item is double-clicked.
On a side note, looping through the controls of the PropertyGrid and assigning Click/Mouse events to them allows this to happen when the property name is clicked, but still does not capture the click when the property value is clicked. Any suggestions?

Combobox selection control not coming out

I'm working with winforms comboboxes. When I select any item in combobox I've raised an event. But control can not come outside. When I scroll the mouse the selected event changing every time.
how to prevent this?
The mouse scrolling actually does change the item selection in the combobox and hence your event is fired, you don't want your event handling code on mouse scroll?
If the problem is that you cannot use the mouse wheel to open drop-down portion of the combobox, then you can use ComboBox.DroppedDown property.

Categories