How to disable mousedown event on Silverlight listbox - silverlight

I have a third-party image control inside an SL5 listbox itemtemplate. This makes for a nice scrollable gallery of images.
Now for the trouble: the third-party image control (LeadTools v17.5) has an interactive feature wherein mouseleftbuttondown causes a draggable magnifying glass to appear. This works great when the control is not hosted in a listbox. But clicking on the control within a listboxitem does nothing. After some research I "believe" this is because the listboxitem is trapping the mouseleftbuttondown event marking it as handled so the image control never sees it. In my application I have no need to handle the mouseleftbuttondown event at the listbox level (other buttons etc control my UI). Assuming I'm correct, is there a way to stop the listboxitem from listenting to this event?
Or perhaps I'm completely wrong about the cause. In that case any other ideas about why the listbox appears to block mouseleftbuttondown events from reaching the controls within is appreciated.
Thanks,
Mark

Instead of trying to keep the ListBoxItem from handling the event, you might be able to use UIElement.AddHandler with handledEventsToo: true, if you can get the necessary UIElement and Delegate references to trigger the image control's feature.

Thanks for your suggestions. In this case it turns out the quick solution was to add this handler to the image control:
private void leadGalleryImageViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
e.Handled = true;
}
From this I gather the mouseleftbuttondown event was being received by the image control all along, but likely as it bubbled up through the listitem and beyond, the listitem did its thing then marked it as handled effectively killing anything the image control was trying to do. By marking the event as handled at the image control level, the listitem ignores it.

Related

parent object mouse events not fired when mouse is over the inner object in silverlight

I have a rectangle in my XAML which acts as container of a textbox (textbox is displayed on the rectangle), I can move rectangle by dragging it (using mousedown and mousemove events of rectangle object), but there is a problem with textbox as it covers a big part of my rectangle, so when users click on mouse (and try to move it), there is a huge chance that they click actually on the textbox, so rectangle mouse events don't fire up and nothing works! (i.e. no move is possible). How can I overcome this problem? (I use SL 5 & VS 2012)
The TextBox control handles the mouse down events and stops them from bubbling up the visual tree. (if you're using a canvas though bubbling events won't help)
You can add your own event handler to the mouse events through code e.g.
txtbox.AddHandler(TextBox.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(<your handler>), true);
where your handler has the signature:
private void <your handler>(object sender, MouseButtonEventArgs e)
{
}
Let me know if the particulars of your situation require something more than this.

How do I find out where the focus is going in my WPF application?

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.

Custom event for wpf UIElement

Is is possible to fire event when a UIElement's location is changed in wpf? We can fire location changed event in case of Windows but can we have a custom event which fires when a UIElement's location is changed in the Window.
It's not possible for the general case. The UIElement doesn't even know the location it's rendered to.
You can do it for particular cases, though. You can use events of the layout parent, like the Left and Top attached property of the Canvas, the scroll offset of a ScrollViewer, etc., depending on where your UIElement is in the visual tree.
Maybe this link will help you, UIElement supports a lot of Events. I think what you are looking for are Manipulation Events or the LayoutUpdate Event.
MSDN UIElement Class

DrawingVisual selection working - deselecting, not so much

ok, I got the several shapes in my custom FrameworkElement to allow for hit testing selection. Next comes the task of deselecting all shapes if the user clicks on a blank area. This doesn't work by default because the FrameworkElement doesn't fire a mousedown event if you click on a "blank" area.
Do people solve this by putting a background rectangle as the first drawingvisual in their frameworkelements (that will accept clicks, but will be treated differently than clicking on the foreground objects), or do they handle the "empty" mousedown events in the class that constructs the FrameworkElement (which in my case is a Viewmodel in an MVVM setup)? Or a third way I'm not considering?
thank you
Try setting the background to transparant and you most likely will get mouse down events.
Instead of deriving from FrameworkElement, derive your control from Control class. The Control class has the Background property which you will set to transparant to get mouse down events. See below link for comments about deriving directly from FrameworkElement:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx

How do I get the KeyDown event to fire in a custom container control?

I have a custom container control (deriving from FlowLayoutPanel) which contains zero or more child controls dragged there by the user. When a child control is clicked, it is "selected." (It is drawn with a colored border and options are available for altering its properties.)
I would like to handle the Delete key so that, if the user is currently working in the container control (clicked within the control or on a child control, for instance) the currently selected control (if there is one) is deleted.
I already have the delete functionality working using a right-click context menu on the child items. My problem is handling the Delete key. I cannot figure out how to get the KeyDown event to raise within my container control. I know it has something to do with focus, so that Control.Select() (or its equivalent) must be called, but what is the best way to go about this? Where does this focus logic reside? Or is there a better way?
I do not want to handle the KeyDown event in the form and then sniff out where the focus is. This is a reuseable container control and I want the logic to reside there.
What do I have to do to get the KeyDown event to fire on a custom control?
public class MyContainer : FlowLayoutPanel
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("How do I get here?");
e.Handled = true;
}
base.OnKeyDown(e);
}
}
The KeyDown event is listed as unmeaningful for the FlowLayoutPanel control on MSDN. Suggest the PreviewKeyDown event as an alternative.
Is it possible that the items dragged into the container are receiving the event?
Perhaps after an item is drug into your container, you need to manually set the focus to the container.

Resources