I have a ScatterView in which I can add custom scatter view items. They are represented in a grid containing two viewbox:
One for the custom shape
One for a TextBlock (elmtNameTextBlock)
The TextBlock can be modified by double clicking on it: it opens a window with a simple TextBox which closes on "Enter" press and updates elmtNameTextBlock.
As my program has to be designed for surface. I have to handle a DoubleTap event for that. I saw two methods for that:
DoubleTap Event
DoubleTapped Event
I tried to add both event:
public event EventHandler<GestureEventArgs> DoubleTap;
and
public event DoubleTappedEventHandler DoubleTapped;
but my program cannot find GestureEventArgs while I have the correct namespace and DoubleTappedEventHandler.
Have I for only choice to handle the DoubleTap event manually ?
Related
do you know a "simple" way to detect when a user control loses focus?
I am creating a console output inspired by Visual Studio.
Visual Studio console output collapses when I click anywhere.
Obviously in my application if I click on a Panel (which has no focus) I do not receive any events.
I had thought about using the mouse_down event on the application, but I would have to do an evaluation with every click!
Do you know any way to know when you click out of your control?
UserControl has a LostFocus event. In the code behind of your UserControl you can simply add an event handler.
public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
LostFocus += OnFocusLost;
}
private void OnFocusLost(object sender, RoutedEventArgs e)
{
// Do something here.
}
}
If you are referencing it from another class then you can create an event handler on that class and subscribe to the event from the UserControl.
UserControl control = new UserControl();
control.LostFocus += OnFocusLost;
With that being said, if you have a TextBox or some other control inside of the UserControl then the UserControl will lose focus every time a TextBox gets the focus so you will most likely need to write a method to determine if any of the elements inside the UserControl have focus.
I have a user control that contains a textbox and some buttons.
I can't respond to the text changed event from the textbox from outside of that user control (say from the parent form that the control is placed on).
How can I get an event that raised when the text has changed from outside of the user control?
Please see this answer on how to create a custom event in WPF.
Following this approach, you can handle the "TextChanged" event for the textbox inside your user control, then raise a custom event from there.
in your textcontrol event change, put this:
Me.OnTextChanged(e)
Or create your own event
Upper code:
Public Event ClaveModificada(Causa As String)
and when you need:
RaiseEvent ClaveModificada("")
I have a user control that I'm trying to make draggable. The whole control should be draggable except when you click on buttons or text boxes. I'm handling the mousedown, mouseup and mousemove events on the usercontrol itself and I can drag by clicking anywhere. The only issue is now I can't click any buttons on the user control. Any clue what's going on?
Code is something like this:
<UserControl PreviewMouseLeftButtonDown="Popup_PreviewMouseLeftButtonDown" ....STUFF...>
<!-- CAN'T CLICK THIS -->
<Button />
<UserControl>
Code Behind:
public void Popup_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldMousePosition = this.PointToScreen(e.GetPosition(this));
this.Popup.Child.CaptureMouse();
}
The issue arises when you use CaptureMouse() - this permanently captures all your mouse input on the window, and makes it so that you're unable to click on anything within the Window. I don't know if it's different for you, or if you checked, but it's not (just) that the Button is unclickable - it's that literally everything on the Window is unclickable.
You have to actually do something with the mouse after you've captured it, and then once you finish that, you have to return normal control by calling Mouse.Capture(null). For you, this would probably be best to do in your MouseUp() method.
However, this still leaves the child problem. I can't really think of any way you're going to be able to both capture all mouse click events on a parent control and allow them to get to the child control. I suppose you could check the mouse position against the button position, both relative to the UserControl, then route the click event to the Button every time, but this seems a little overelaborate. Is there a reason you can't just add a full-sized Grid to the UserControl with a lower ZIndex than the Button, and just use that to detect if a click was made inside the UserControl but not on the Button?
I am using visual studio. In my program I have the option for a user to activate certain widgets that they want to have. Each widget is a UserControl, so when they click to show a widget, it is activating the corresponding user control. Once they activate the control, they can drag the boxes wherever they wish so that they can customize how their screen looks. However, whenever you drag one widget over another, it just stacks them on top of each other, and this is not what I want. If I am dragging a box and it touches another one, I want it to stop and not be able to overlap it. How can I do this? I suspect it has something to do with the "DragOver" event, but not for sure.
The DragOver event will be raised on these case :
[+] If the user moves out of a window, the DragLeave event is raised.
[+] If the mouse enters another control, the DragEnter for that control is raised.
[+]If the mouse moves but stays within the same control, the DragOver event is raised.
If this is the event that you want, so try to terminate the widget (User Control) on UserControl_DragOver.
You can terminate your UserControl like this :
private void Btn_Cancel_Click(object sender, EventArgs e)
{
this.Parent.Controls.Remove(this);
}
(If you mean sth else, tell in comment)
I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}