I have a ContextMenuStrip where one of the items has a DropDownItems property that is a collection of dynamically added ToolStripMenuItem objects. When I handle the sub-item Click event, the sender is of type ToolStripMenuItem, but its Owner is a ToolStripDropDownMenu. I can't find how to determine the 'host' ContextMenuStrip from this. It has no Owner property of its own, and Parent returns null.
When I use this adaptation of the code posted by #Steve below:
Dim dropDownItem = DirectCast(sender, ToolStripDropDownItem)
Dim menu As ContextMenuStrip = DirectCast((((dropDownItem.DropDown).OwnerItem).OwnerItem).Owner, ContextMenuStrip)
Dim grid = menu.SourceControl
then menu.SourceControl is Nothing, yet when I handle a top level, i.e. non-dropdown menu item's click like this
Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim strip As ContextMenuStrip = DirectCast(item.Owner, ContextMenuStrip)
Dim grid As DataGridView = DirectCast(strip.SourceControl, DataGridView)
then I get the grid I was looking for.
If I understand correctly, you want to reach the ContextMenuStrip object from inside an Click event of a ToolStripMenuItem belonging to a ToolStripDropDownMenu.
If this is the case then
private void TestToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripDropDownItem x = sender as ToolStripDropDownItem;
if (x != null)
{
ContextMenuStrip k = (((x.DropDown).OwnerItem).OwnerItem).Owner as ContextMenuStrip;
k.ForeColor = Color.Red; // as an example.
}
}
Related
how to get the Hidden Columns Control Value in PreparingCellForEdit Silverlight Datagrid
Code as Follows:
Private Sub TaskDataGrid_LoadingRow(ByVal sender As System.Object, ByVal e As
System.Windows.Controls.DataGridRowEventArgs)
Dim row As DataGridRow = e.Row
Dim cellContent As FrameworkElement = TaskDataGrid.Columns(8).GetCellContent(e.Row)
Dim cboLabValidated As ComboBox = CType(cellContent.FindName("cboLabValidated"), ComboBox)
Dim ViewModel As New NonFirmWareNewRequestViewModel()
If cboLabValidated IsNot Nothing Then
cboLabValidated.ItemsSource = ViewModel.YesNoValues
End If
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
in the above code I am hidden the column 1 in LoadingRow Event and in need to get the value of that Column in PreparingCellForEdit
Code for PreparingCellForEdit as Follows:
Dim fe As FrameworkElement = TaskDataGrid.Columns(5).GetCellContent(e.Row)
Dim fe1 As FrameworkElement = TaskDataGrid.Columns(1).GetCellContent(e.Row)
Dim gridCmbo As Grid = DirectCast(fe, Grid)
Dim gridCmbo1 As Grid = DirectCast(fe1, Grid)
Dim lbltaskId As Label = CType(gridCmbo1.FindName("lbltaskId"), Label)
Dim cboCompVerSel As ComboBox = CType(gridCmbo.FindName("cboCompVerSel"), ComboBox)
Dim lblCompVer As Label = CType(gridCmbo.FindName("lblCompVer"), Label)
I am using label control to show column 1 and I am identifying the label control object but the content becomes empty..
this is working fine
1) While in the Loading Row Event We should not hide the Column, if you hide the column you cannot get the value from the column let the data Load first in the LoadingRow Event.
2) Hide the column in the Selection changed event code as follows :
Private Sub TaskDataGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TaskDataGrid.SelectionChanged
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
this event will fire after LoadingRow Datagrid event or PreparingCellForEdit event once the Row is loaded we can hide the column and can get the value where ever you want.If you Hide in LoadingRow event without loading the Data you cannot get the value of that Control in the Datagrid template or Data Column.
I have a ListView and DropDownList(primary) and tow more and a text box, inside every Row of that ListView. All on a user control ,and using the this user control on the other usercontol(here the operations to be performed.)(we are using DNN) .
Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I wanted to show and hide the other dropdowns and textbox depending on a specific value of the DropDrownList(primary) selected.
I am nowhere to do the same..despite the several attempts.
Screening Outcome: 
Comments:
Subset Used: 
TAR Status: 
code behind like this
Public Sub ddlFindingsScreeningOutcome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
Dim txtFindingsComments As TextBox = CType(item.FindControl("txtFindingsComments"), TextBox)
Dim ddlFindingsTarStatus As DropDownList = CType(item.FindControl("ddlFindingsTarStatus"), DropDownList)
If Not ddl Is Nothing Then
If Not ddl.SelectedItem.Text = "Not Screened" Then
txtFindingsComments.Visible = True
ddlFindingsTarStatus.Visible = True
End If
Else
txtFindingsComments.Visible = False
ddlFindingsTarStatus.Visible = False
End If
End Sub
I have a WPF application in which there's a listbox filled with items of type 'Match'.
How do I make the button(contained within the item) actually select the item so that I might extract the value?
Here is my code: neither works since clicking the button doesn't actually select the item
private void LayButton_Click(object sender, RoutedEventArgs e)
{
var x = (Market)ListBoxSelectedMarket.SelectedItem;
var y = (sender as ListBoxItem);
}
Thanks
You should be able to use the DataContext from the clicked Button and get the ListBoxItem container from there, and then select it.
private void LayButton_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
var dataContext = button.DataContext;
ListBoxItem clickedListBoxItem = ListBoxSelectedMarket.ItemContainerGenerator.ContainerFromItem(dataContext) as ListBoxItem;
clickedListBoxItem.IsSelected = true;
}
If you are binding to an object an alternative method could be (in VB)
This then gives you an instance of your object to play with and saves you having any mapping fields on the listbox
Private Sub OTC_Settled_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim pr_YourObject As New YourObject
Dim btn As Button = CType(sender, Button)
OTC = DirectCast(btn.DataContext, pr_YourObject)
End Sub
I haven't done much WPF programming, but you could try getting the parent of the button if it works the same as a WinForms container object.
I have a problem with my TreeView in a WPF application (Framework 3.5 SP1).
It's a TreeVIew with 2 Levels of Data. I expand / collapse the items of the first level in a particular way (with a single mouse-click on the TreeViewItem). Again when I expand a first-level TreeViewItem, I add some second-level TreeViewItems to the group (it's an important detail, infact if I don't add the items the problem doesn't occur). All works good until the TreeView loses focus.
If, for example, I expand the TreeViewItem at the first position, adding at the same time one element to the second-level, then I click on a button (to let the TreeView lose the focus), and then I click again on the TreeViewItem at the third position to expand it, the TreeViewItem that results from the hit-test with the mouse position is not the "real" TreeViewItem (in this case the third), but a TreeViewItem which is in an higher position than the one clicked (in this case the second).
I have tried to use the UpdateLayout method on the TreeView-LostFocus event, but without results. Probably I need a method that does the opposite: starting from the UI, refresh the object that contains the position of the TreeViewItems.
Can you, please, help me?
Thank you!
Pileggi
This is the code:
' in this way I tried to put remedy at the problem, but it doesn't work.
Private Sub tvArt_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles tvArt.LostFocus
Me.tvArt.UpdateLayout()
e.Handled = True
End Sub
' here I expand / collapse the items of the first level of my TreeView
Private Sub tvArt_PreviewMouseUp(ByVal sender As System.Object, ByVal e As MouseButtonEventArgs) Handles tvArt.PreviewMouseUp
Dim p As Point = Nothing
Dim tvi As TreeViewItem = getItemFromMousePosition(Of TreeViewItem)(p, e.OriginalSource, Me.tvArt)
If tvi Is Nothing = False Then
If tvi.HasItems Then
Dim be As BindingExpression = BindingOperations.GetBindingExpression(tvi, TreeViewItem.ItemsSourceProperty)
Dim ri As P_RicambiItem = DirectCast(be.DataItem, P_RicambiItem)
If ri.isExpanded = False then
' here I add items to the second level collection
End If
ri.isExpanded = Not ri.isExpanded
End If
End If
e.Handled = True
End Sub
Private Function getItemFromMousePosition(Of childItem As DependencyObject)(ByRef p As Point, ByVal sender As UIElement, _
ByVal _item As UIElement) As childItem
p = sender.TranslatePoint(New Point(0, 0), _item)
Dim obj As DependencyObject = DirectCast(_item.InputHitTest(p), DependencyObject)
While obj Is Nothing = False AndAlso TypeOf obj Is childItem = False
obj = VisualTreeHelper.GetParent(obj)
End While
Return DirectCast(obj, childItem)
End Function
I have find this solution (but I don't like it very much). The problem is depending from the added items that wpf, for some reasons, doesn't remember that exist. Then I do a "manual" refresh with a method that clear and re-add all the items in the source-collection:
Public Sub RefreshData(ByVal RicambiListPass As ObservableCollection(Of P_RicambiItem))
Dim l As New List(Of P_RicambiItem)
l.AddRange(RicambiListPass)
_RicambiList.Clear()
For Each i As P_RicambiItem In l
_RicambiList.Add(i)
Next
End Sub
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