Name of control in routed event - wpf

Isn't there a way to tell what control fired a routed event? I have a SelectionChangedEvent for use by a combobox on a radgridview. I want the coding in that event to handle only that combobox and no others. I tried using e.OriginalSource.Name, ToString, sender.ToString, sender.Name but all return "". So there's no way to tell what combobox is being processed by the event.
Code to create event:
Me.AddHandler(RadComboBox.SelectionChangedEvent, New System.Windows.Controls.SelectionChangedEventHandler(AddressOf FinishedEndsChanged))
Code inside event:
Private Sub FinishedEndsChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
Try
If dgChosenItems.SelectedItems.Count > 0 Then
Dim comboBox As RadComboBox = CType(e.OriginalSource, RadComboBox)
If comboBox.SelectedValue IsNot Nothing Then
Dim endChosen As String = CStr(comboBox.SelectedValue)
Thanks.

Give your ComboBox a name so you can address it in sourcecode-behind by this unique name. Check
If e.OriginalSource == _youridhere_ Then // If sender == ... should work as well
// do what you must
Not very good style and probably only feasable for one to a few boxes...

I believe I found the answer here:
[https://www.telerik.com/forums/selection-changed-event-for-gridviewcombobox-column]
I chose to use SelectedValudPath.
Thanks, Patrick, for taking the time to reply.

Related

Need to determine which grid a double-click came from?

I think this should be fairly simple, but I've been looking through the properties in the signature for the handler that I'm using and I don't see any way to suss out what I'm looking for.
I have a fairly simple WPF app with two DataGrid controls in the same window. I have a double click event defined in the XAML like so:
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter
Event="MouseDoubleClick"
Handler="Row_DoubleClick"/>
</Style>
</DataGrid.ItemContainerStyle>
And in the code behind (do we call it that in WPF apps?) I have the Row_DoubleClick handler set up like so:
Private Sub Row_DoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Now the sub itself works fine and picks up the row that was double-clicked just fine. However, as I noted before I have two DataGrids that use this same sub for the double-click event. I realize one path might be to simply make two subs, but it seems like I should be able to use the one for both, and it's taking the exact same action in either case, just using the row from one DataGrid or the other.
It always defaults to the first, let's call it IncompleteGrid, if a row is selected even if the second DataGrid, let's call it CompleteGrid, is the the one being double clicked. I've been looking through the sender and e objects in debug mode, but I don't see any place or property I can check to see which grid the double-click is coming from.
Any ideas?
You can get the parent dataGrid from row by using VisualTreeHelper. Have this private method on your code (code is in C#, hope you can get it convert to VB easily):
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
DataGrid senderDataGrid = FindAncestor<DataGrid>(row);
}
private T FindAncestor<T>(DependencyObject dependencyObject)
where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor<T>(parent);
}
VB Version:
Private Sub Row_DoubleClick(sender As Object, e As MouseButtonEventArgs)
Dim row As DataGridRow = TryCast(sender, DataGridRow)
Dim senderDataGrid As DataGrid = FindAncestor(Of DataGrid)(row)
End Sub
Private Function FindAncestor(Of T As DependencyObject)(dependencyObject As DependencyObject) As T
Dim parent = VisualTreeHelper.GetParent(dependencyObject)
If parent Is Nothing Then
Return Nothing
End If
Dim parentT = TryCast(parent, T)
Return If(parentT, FindAncestor(Of T)(parent))
End Function
This parameter should give you the information:
ByVal sender As System.Object
sender should be the grid that the double-click is coming from. (That's the meaning of sender -- the control that sent the event.)
You can cast sender to a DataGrid if you want to do specific stuff with it.
Edit: If sender is a DataGridRow instead of a DataGrid, then you could use this question to find the host DataGrid. (Using a RelativeSource or a CommandParameter seems to the accepted methods for this.)

Hide the DataGrid while TextBox Loses its Focus If DataGrid is not Focused

I have a textbox and DataGrid
While textbox loses the focus and if DataGrid is not Focused then I want to Hide the DataGrid.
I use the below code.
Private Sub txt_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles txt.LostFocus
If DataGrid1.IsFocused = False Then
DataGrid1.Visibility = Windows.Visibility.Hidden
End If
End Sub
using this code even if I click on any Item on DataGrid the DataGrid hides.
Is there any problem in my code?
I'm not sure what the problem is... the behaviour you describe is consistent with your code.
The behaviour might be different from what you would expect...
I think when the TextBox loses focus the DataGrid will never have focus because the TextBox didn't finish losing focus. Is that the problem?
If that's the problem, you may add some kind of delay before hiding DataGrid (in a non blocking way of course). You can create a new Thread, do a Sleep(500) on that thread before hiding the control, and see what happens.
You also need to take care because only the UI thread may change visible controls, but you may ask further help if you choose to do that.
I hope it helps.
When the textbox lostfocus even fired .. the gridview not focused yet ..
So, add something like this
Dim lDGVFocused as Boolean
Private Sub Datagrid1_Enter( ... ) ...
lDGVFocused = True
End Sub
Private Sub Datagrid1_LostFocus( ... ) ...
lDGVFocused = False
End Sub
Private Sub txt_LostFocus( ... ) ...
If not lDGVFocused then DataGrid1.Visible = False
End Sub
Private Sub txt_GotFocus( ... ) ...
DataGrid1.Visible = True
End Sub

Apply and validate a bound DataGridViewComboBoxCell directly upon selection change

I have a windows forms DataGridView that contains some DataGridViewComboBoxCells that are bound to a source collection using DataSource, DisplayMember and ValueMember properties. Currently the the combobox cell commits the changes (i.e. DataGridView.CellValueChanged is raised) only after I click on another cell and the combobox cell loses focus.
How would I ideally commit the change directly after a new value was selected in the combobox.
This behaviour is written into the implementation of the DataGridViewComboBoxEditingControl. Thankfully, it can be overridden. First, you must create a subclass of the aforementioned editing control, overriding the OnSelectedIndexChanged method:
protected override void OnSelectedIndexChanged(EventArgs e) {
base.OnSelectedIndexChanged(e);
EditingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
EditingControlDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
This will ensure that the DataGridView is properly notified of the change in item selection in the combo box when it takes place.
You then need to subclass DataGridViewComboBoxCell and override the EditType property to return the editing control subclass from above (e.g. return typeof(MyEditingControl);). This will ensure that the correct editing control is created when the cell goes into edit mode.
Finally, you can set the CellTemplate property of your DataGridViewComboBoxColumn to an instance of the cell subclass (e.g. myDataGridViewColumn.CellTemplate = new MyCell();). This will ensure that the correct type of cell is used for each row in the grid.
I tried using Bradley's suggestion, but it was sensitive to when you attached the cell template. It seemed like I couldn't allow the design view to wire up the column, I had to do it myself.
Instead, I used the binding source's PositionChanged event, and triggered updates from that. It's a little bit odd, because the control is still in edit mode, and the databound object doesn't get the selected value yet. I just updated the bound object myself.
private void bindingSource_PositionChanged(object sender, EventArgs e)
{
(MyBoundType)bindingSource.Current.MyBoundProperty =
((MyChoiceType)comboBindingSource.Current).MyChoiceProperty;
}
A better way to achieve this that I am using successfully rather than subclassing or the somewhat inelegant binding source method above, is the following (sorry it's VB but if you can't translate from VB to C# you have bigger problems :)
Private _currentCombo As ComboBox
Private Sub grdMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdMain.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
_currentCombo = CType(e.Control, ComboBox)
AddHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
End If
End Sub
Private Sub grdMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMain.CellEndEdit
If Not _currentCombo Is Nothing Then
RemoveHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
_currentCombo = Nothing
End If
End Sub
Private Sub SelectionChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myCombo As ComboBox = CType(sender, ComboBox)
Dim newInd As Integer = myCombo.SelectedIndex
//do whatever you want with the new value
grdMain.NotifyCurrentCellDirty(True)
grdMain.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
That's it.

Handling events from user control containing a WPF textbox

In order to take advantage of the spell checking ability of WPF textboxes, I have added one to a user control (with the use of elementhost). This user control is used in various window forms. My current problem is trying to handle keyup events from this textbox but the windows form is unable to "get" any event from the control. I can access the properties of the textbox just fine (i.e. text, length, etc.) but keyboard events don't seem to work.
I have found, however, that the following will bring back events from the WPF textbox:
Public Class MyUserControl
Private _elementHost As New ElementHost
Private _wpfTextbox As New System.Windows.Controls.Textbox
Private Sub MyUserControl_Load(...) Handles Me.Load
Me.Controls.Add(_elementHost)
_elementHost.Dock = DockStyle.Fill
_elementHost.Child = _wpfTextbox
Dim MyEventInfo As EventInfo
Dim MyMethodInfo As MethodInfo
MyMethodInfo = Me.GetType().GetMethod("WPFTextbox_KeyUp")
MyEventInfo = _wpfTextBox.GetType().GetEvent("PreviewKeyUp")
Dim dlg As [Delegate] = [Delegate].CreateDelegate(MyEventInfo.EventHandlerType, Me, MyMethodInfo)
MyEventInfo.AddEventHandler(_wpfTextBox, dlg)
End Sub
Public Sub WPFTextbox_KeyUp(ByVal sender As Object, ByVal e As RoutedEventArgs)
' something goes here
End Sub
End Class
The user control is now able to do something after the PreviewKeyUp event is fired in the WPF textbox. Now, I'm not completely sure how to have the window form containing this user control to work with this.
Im a C# person not VB so please bear with me.. Basically you could assign the event from your Window rather than within your UserControl.. So in the constructor of your Window assign the PreviewKeyUp:
this.myUserContorl.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(WPFTextbox_KeyUp);
then place the event handler in your Window:
private void WPFTextbox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
}
Incidentally you needn't go through the hassle of capturing the event in your UserControl as you can still access your TextBox within you UserControl directly from your Window (if you make it public), again from your constructor in your Window:
this.myUserContorl.wpfTextbox.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(WPFTextbox_KeyUp);
I imagine it would look like this in VB (at a guess):
AddHandler myUserContorl.wpfTextbox.PreviewKeyUp, AddressOf WPFTextbox_KeyUp
ElementHost has a static method called EnableModelessKeyboardInterop(). Try calling it?
ElementHost.EnableModelessKeyboardInterop();
Read more here
Seems basic, but did you set the KeyPreview to TRUE on your form?

How to add a ContextMenu depending on which WPF DataGrid row is right-clicked?

I need to display different options in a ContextMenu depending on which row of a WPF DataGrid is right-clicked. My initial ideas were to accomplish this through either binding or handling a mouse click event, but I haven't had success with either strategy so far. Any help would be most appreciated!
Thank you!
Denise
You can handle the DataGrid's ContextMenuOpening event and based on the original source of the routed event you adjust your context menu.
Below is a sample where I show a context menu if the data context of the original source is of type Inventory otherwise I do not show the context menu by handling the event.
Private Sub InventoriesDataGrid_ContextMenuOpening( _
ByVal sender As Object, _
ByVal e As System.Windows.Controls.ContextMenuEventArgs) Handles _
InventoriesDataGrid.ContextMenuOpening
Dim context = DirectCast(e.OriginalSource, System.Windows.FrameworkElement).DataContext
If TypeOf context Is Inventory Then
InventoriesDataGrid.ContextMenu = InventoriesDataGrid.Resources("DefaultContextMenu")
Else
e.Handled = True 'Do not show context menu.
End If
End Sub
I'm sure it is too late to help you now, but in case it is not too late and for anyone else who comes across this.
You can try the OriginalSource from the ContextMenuEventArgs argument in the ContextMenuOpening event :
DataGridResults.ContextMenuOpening += (sender, args) =>
{
var frameworkElement = args.OriginalSource as FrameworkElement;
var gridRow = frameworkElement != null ? frameworkElement.TemplatedParent as DataGridRow : null;
}
Note however that the use of TemplatedParent depends on how the datagrid items were bound

Resources