How can I get a current focused control in WPF?
I found some solution for WinForms, but invoking WIN32 API function, didn't work in WPF?
Is there any way for doing it in WPF?
I know this is a late answer, but maybe people searching can find this helpful, I it found on msdn in the "Navigating Focus Programmatically" section close to the bottom of the page:
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
Here's what I did
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
lostFocusControl = e.OldFocus;
}
private void PauseButton_PreviewKeyDown(object sender, KeyEventArgs e)
{
// invoke OnPreviewLostKeyboardFocus handler
}
Related
neither
<WebBrowser x:Name="wbMain" Cursor="None"></WebBrowser>
nor
*{cursor:none}
is working for me. I can't find any resources online telling me how to accomplish this. The use-case for this is an application that runs full screen meant for viewing only after the setup takes place.
Edit: I forgot to add that the css works as expected when viewing the website in the IE9 browser.
I don't know if this is a good or bad practice but you can add System.Windows.Forms reference
then
private void MouseEnter(object sender, MouseEventArgs e)
{
System.Windows.Forms.Cursor.Hide();
}
private void MouseLeave(object sender, MouseEventArgs e)
{
System.Windows.Forms.Cursor.Show();
}
use this code on mouseEnter form example in web-browser control
For those who dont like to add WinForms reference, try
[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);
and call ShowCursor(false) when needed.
In app.cs
protected override void OnStartup(StartupEventArgs e)
{
System.Windows.Forms.Cursor.Hide();
}
Is there a way to create global keys in Silverlight? I added a KeyDown event handler to the topmost Page element. I can catch the key signal when elements like a Button, TextBox, Calendar, and RichTextBox have focus. But the DatePicker won't let me handle the key down event at the Page level. Any way to make a hotkey in non-OOB SL?
P.S. I am assuming that the DatePicker control behaves differently than others as it is still in the Silverlight Toolkit.
have you tried using the AddHandler method? With this method you can add a handler and define if you want to handle already handled events, too.
In my simple example I added it to the RootVisual in the app.xaml.cs.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RootVisual.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(HandleKeyDown), true);
}
private void HandleKeyDown(object sender, KeyEventArgs e)
{
//Add your keyhandling code here
Debug.WriteLine(e.Key);
}
I tried it with a DatePicker and it works.
Hope this helps!
BR,
TJ
This may help you. The author provides two ways using HTML bridge (when your Silverlight does not have focus) and using the KeyUp event on Application.RootVisual.
I have not tried it myself so I wouldn't know if it's working.
Snoop shows that the command is "ApplySingleSpace", but when I try disabling it via the method described in this article . Like this:
<RichTextBox.CommandBindings>
<CommandBinding
Command="ApplySingleSpace"
CanExecute="BlockTheCommand"/>
</RichTextBox.CommandBindings>
.
private void BlockTheCommand(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
My app crashes because there is no ApplySingleSpace command. ApplySingleSpace is not in the EditingCommands either.
What am I missing?
Unfortunately that will not work for
me. The reason I am trying to disable
the command is that I have a
KeyBinding in a higher nested view
that is not firing because the CTRL+1
gesture is being swallowed by the
richtextbox which has keyboardfocus.
How about overwriting that KeyBinding with a custom command that does what you want instead of trying to somehow disable it?
<RichTextBox.InputBindings>
<KeyBinding Command="local:YourCommands.Cmd1" Gesture="CTRL+1" />
<RichTextBox.InputBindings>
Taken from this question.
Using the code from this answer
How can I programmatically generate keypress events in C#?
to refire all events on PreviewKeyDown other than those you want handled by the richtextbox seems to work for me. (I only need Ctrl-C for copy). Of course you could make it so it only refires Ctrl-1 if that's what you need.
private void logKeyHandler(object sender, KeyEventArgs e)
{
if (!(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C))
{
e.Handled = true;
var routedEvent = Keyboard.KeyDownEvent;
this.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromDependencyObject(this),
0,
e.Key) { RoutedEvent = routedEvent }
);
}
}
What about trying with the gesture instead...
<RichTextBox.InputBindings>
<KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
</RichTextBox.InputBindings>
I'm building a Windows Presentation Foundation control with Microsoft Blend.
When I leave my control by pressing the left-mouse-button, the MouseLeave-Event is not raised. Why not?
This is intended behaviour: When you are doing mousedown on a control and leaving the control, the control STILL retains its "capture" on the mouse, meaning the control won't fire the MouseLeave-Event. The Mouse-Leave Event instead will be fired, once the Mousebutton is released outside of the control.
To avoid this, you can simple tell your control NOT to capture the mouse at all:
private void ControlMouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
Control control = (Control) sender;
control.Capture = false; //release capture.
}
Now the MouseLeave Event will be fired even when moving out while a button is pressed.
If you need the Capture INSIDE the Control, you need to put in more effort:
Start tracking the mouseposition manually, when the mousekey is pressed
Compare the position with the Top, Left and Size Attributes of the control in question.
Decide whether you need to stop the control capturing your mouse or not.
public partial class Form1 : Form
{
private Point point;
private Boolean myCapture = false;
public Form1()
{
InitializeComponent();
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
myCapture = true;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (myCapture)
{
point = Cursor.Position;
if (!(point.X > button1.Left && point.X < button1.Left + button1.Size.Width && point.Y > button1.Top && point.Y < button1.Top + button1.Size.Height))
{
button1.Capture = false; //this will release the capture and trigger the MouseLeave event immediately.
myCapture = false;
}
}
}
private void button1_MouseLeave(object sender, EventArgs e)
{
MessageBox.Show("Mouse leaving");
}
}
of course you need to stop the own tracking ( myCapture=false;) on MouseUp. Forgot that one :)
When I don't get mouse events I expect I typically use Snoop to help me understand what is happening.
Here are a couple of links:
1- Snoop (a WPF utility)
2- CodePlex project for Snoop
And for completeness and historical reasons (not the bounty - it doesn't make sense having two duplicate questions - you should probably move it into one if not too late)...
I made a thorough solution using global mouse hook here (approach 2)
WPF: mouse leave event doesn't trigger with mouse down
And simplified its use - you can use it by binding to commands in your view-model - e.g.
my:Hooks.EnterCommand="{Binding EnterCommand}"
my:Hooks.LeaveCommand="{Binding LeaveCommand}"
my:Hooks.MouseMoveCommand="{Binding MoveCommand}"
...more details in there
Old question but I came across the same problem with a Button (MouseLeave does not fire while MouseDown because MouseDown Captures the Mouse...)
This is how I solved it anyway:
element.GotMouseCapture += element_MouseCaptured;
static void element_MouseCaptured(object sender, MouseEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
element.ReleaseMouseCapture();
}
Hope that helps someone looking for a quick fix :P
I have a problem using custom made UserControl in Silverlight Page.
The UserControl is generally a rectangle containing a smaller rectangle inside.
I want to use the UControl in a Silverlight MainSite.
i've implemented a method on mouse button down for a smaller rectangle called in here Button1:
public void Button1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("Hello");
}
How can i use it from a MainSite? From there I can only implement a method like:
private void ImportedControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
firstLeaf.Button1_MouseLeftButtonDown(sender, e);
}
I can not implement a method for Button1.
How can i mek this Work?
HELP:)
I am still not sure I understand the question, but let me take a swing at an answer. Why don't you use a real Button and just template it with a rectangle? This way you get all the benefits of actually having a button while having it look like a rectangle, including the Click event.
Here is a post Scott Guthrie did on control templating with a button.