How to use the context menu of the parent in a textbox - wpf

I have a listbox with several controls. Each control contains an custom autocompletebox which contains a System.Windows.Controls.AutoCompleteBox. When I right click on the control a custom contextm menu shows up. But with a right click on the textbox there appears the default contextmenu of the TextBox (with Copy, Cut and Paste).
My goal is to show my custom context menu with a right click on the TextBox.
Further informations:
My custom context menu is defined in the DataTemplate of the ListBox but I could define it in the Ressources or somewhere else as well.
I tried:
- when I null the context menu of the custom autocompletebox or the System.Windows.Controls.AutoCompleteBox of it, there is no effect at all
Thanks for every help ;)

You can either bind the context menu property to parent element's context menu or bind to the context menu once you define in the resource.xaml

Try using PreviewMouseDown instead of MouseDown to handle the MouseDown event.
In XAML:
<ListBox Margin="3" PreviewMouseDown="MouseDownOnListBox">
In Code:
private void MouseDownOnListBox(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
{
//Display your context menu
}
}
If you are using PreviewMouseDown on a list, when you click anywhere on the list, this event will be triggered first.

Related

How do I get the UI object inside of a content presenter?

I have an ItemsControl displaying a list via binding. The list is of a ViewModel type which is then referenced in a DataTemplate to display a button. The button has it's UID bound to a GUID from the the view model. When a new view model is added to the bound list, I need to get hold of the button that will be added, as the buttons need to be able to be dragged/dropped by the user. At the moment the closest I can get is finding the ContentPresenter that displays the button, but the content of that ContentPresenter is of type view model.
Is there a way to find the button that has been added? Or should I not used a DataTemplate and create the buttons my self in order to access them?
I have used the VisualTree helper to get the content presenter, but have not managed to find the button.
You could handle the Loaded event for the Button:
<DataTemplate>
<Button Loaded="OnButtonLoaded" ... />
private void OnButtonLoaded(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
//...
}
You won't be able to get a reference to it using the VisualTreeHelper until it has actually been added to the visual tree and loaded anyway.

Show ContextMenu for TreeViewItem by raising events in code

I want to write a coded UI test for my treeview with context menu. The idea is to validate if the context menu is added to treeViewItem.
So far I tried to raise PreviewMouseRightButtonUp event. This is not working.
If I subscribe to the event then I get a callback after right-clicking the tree item. But raising the event manual does not cause the context menu to be added.
Here is how I am raising the event:
MouseButtonEventArgs eventArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Right);
eventArgs.RoutedEvent = Mouse.MouseUpEvent;
uiElement.RaiseEvent(eventArgs);
If you want to validate if the context menu is added to treeViewItem, why not right-click on the tree item and look for the existence of a menu in the window.

Prevent control loosing focus when child gets focus

Ok so I have an issue where I have been trying to create a custom control in Silverlight. It is simply a button that when pressed opens a dropdown menu, however the dropdown menu is a child control. I have a property (isDropDownOpen) that controls whether the dropdown is open. I want that when the control loses focus that the property goes to false so implemented the following override in my class.
protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
{
base.OnLostFocus(e);
Object focusedElement = FocusManager.GetFocusedElement();
FrameworkElement element = focusedElement as FrameworkElement;
if (element != null)
{
IsDropDownOpen = false;
}
}
The problem is that the control looses focus when the child control gains focus so the menu closes as soon as I click on anything other than the button. I can’t really see how to work around this, any ideas?
EDIT: Essentially what I wish to do is check if the item is a child before actually changing the property. In wpf I would do something using 'IsChild()' or '.containsFocus()' however these do not appear to e available in silverlight...
Using silverlight 5.0.
I found the following link to be a solution to my issue.
http://icircusmonkey.wordpress.com/2012/08/26/silverlight-how-to-close-the-popup-when-user-clicks-outside-of-the-control/
The solution in my case was to find the ancestor of the control (going up to the window) and subscribing an event handler to close the menu when a click is registered on the window/ancestor. Works perfectly.

WPF combobox-like custom control

I would like to create custom control that will look like standard WPF ComboBox, but instead of instead of having an ItemsPresenter in the popup there will be another custom control. So, I created a new class that derives from System.Windows.Controls.Control, added a IsDropDownOpen property and created a style that is actually a copy of default ComboBox style (main idea is that the Popup.IsOpen and ToggleButton.IsPressed properties are bound to the IsDropDownOpen property of the control).
The problem is that the Popup is not closed when I click outside of the control.
I took a look at the ComboBox class in the Reflector and found out that ComboBox used some logic to update the IsDropDownOpen property when it loses mouse capture. But that code uses some internal classes. Is there any alternative way to determine if the user clicked outside of the control and close the Popup?
UPD: I didn't find the way to attach a file to post, so I uploaded sample project here
There is a custom control that looks like ComboBox, but it has a TreeView in a popup. When you open popup and click outside of the control it closes automatically, but if you open popup, expand 'Item2' and then click outside the popup isn't closed. The question is how to fix this?
There is the Control.LostFocus event, maybe handling that would be sufficient for this.
This code solves the problem.
In the static contructor:
EventManager.RegisterClassHandler(typeof(CustomComboBox), Mouse.LostMouseCaptureEvent, new MouseEventHandler(OnMouseCaptureLost));
Event handler implementation:
private void OnMouseCaptureLost(object sender, MouseEventArgs e)
{
if (Mouse.Captured != _container)
{
if (e.OriginalSource != _container)
{
Mouse.Capture(_container, CaptureMode.SubTree);
e.Handled = true;
}
}
}

focus does not enter targeted user control properly

I've got a user control, consisting of a text box and two buttons.
the control is placed on a dialog box and when i tab over the dialog
controls, I experience proper behavior - first the text box is focused,
then one button, then the other.
However, when I set the user control as a target of a keyboard shortcut
set with "_" for a label (say press alt+r for "_Row count") the user control
does not receive any focus. Tried implementing "gotkeyboardfocus" and setting
the focus to the textbox control there but it doesn't work.
A UserControl isn't focusable by default so you have to turn it on in order to get this to work.
<my:UserControl1 x:Name="userControl11" Focusable="True" .../>
<Label Target="userControl11">_Row count</Label>
And then you can focus the desired TextBox inside of the UserControl when it recieves Focus
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
textBox.Focus();
}
Hi I recently encountered focus problem. What I did is create a method called SetFocus() from inside my user control. Then inside that usercontrol I set the focus directly to my textbox control after calling .SetFocus() from consumer.
public void SetFocus()
{
this.txtCommand.Focus();
}

Resources