I need to perform some operations on the keydown event on a wpf hyperlink.
I have a simple richtextbox in which i have a hyperlink. I want Keydown event to be fired only when the focus is on the hyperlink, that is the cursor is on the hyperlink text.
Doing this doesn't work and i couldn't find any explanation of why this doesn't work.
<Hyperlink KeyDown="Hyperlink_KeyDown">
test
</Hyperlink>
I would really appreciate it if you could help me.
Thanks.
Have a good day,
Astig.
it doesn't work because hyperlink isn't recognized like focused, you may catch this event in parent control for example grid but before it will be caught you must click on it.
So you may catch window's keydown event like this:
XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Name="MW" KeyDown="MW_KeyDown">
<Grid>
<TextBlock>
<Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate">
Focus it and key down
</Hyperlink>
</TextBlock>
</Grid>
and code:
private void MW_KeyDown(object sender, KeyEventArgs e)
{
if (HL1.IsMouseOver == true)
HL1_RequestNavigate(HL1,new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
}
private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
Edit
Also you can set focus to hyperlink like that:
XAML:
<Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate" KeyDown="HL1_KeyDown" MouseEnter="HL1_MouseEnter">
code:
private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
private void HL1_KeyDown(object sender, KeyEventArgs e)
{
HL1_RequestNavigate(HL1, new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
}
private void HL1_MouseEnter(object sender, MouseEventArgs e)
{
HL1.Focus();
}
Related
So i have the TextBox:
<TextBox Controls:TextBoxHelper.ClearTextButton="True"
LostFocus="textboxNewValueCell_LostFocus"
TextChanged="textboxNewValueCell_TextChanged"/>
And when press on Clear button i want to catch the event.
Is it possible ?
I did not find any event
The ClearTextButton simply calls Clear() on the TextBox. There is no specific event raised. The best you can do is to handle the TextChanged event:
private void textboxNewValueCell_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Length == 0)
{
//the TextBox was cleared and the Button was maybe clicked...
}
}
First, give your TextBox a name. Then, create a click event on the Button. when the click event fires, handle the clearing of the TextBox in the CodeBehind.
<Grid>
<TextBox x:Name="MyTextBox" Text="Some Text"/>
<Button x:Name="ClearButton" Click="ClearButton_Click"/>
</Grid>
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
MyTextBox.Text = string.Empty;
}
I've seen answers to the question of how to break out of a while loop with a keypress for a console app and a winforms app but not a WPF app. So, uh, how do you do it? Thanks.
Okay, let's elaborate:
Something like this doesn't work in a WPF (non-console) app. It throws a runtime error:
while(!Console.KeyAvailable)
{
//do work
}
MainWindow.xaml:
<Window x:Class="WpfApplication34.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<TextBlock x:Name="tb" />
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow:Window {
private int _someVal = 0;
private readonly CancellationTokenSource cts = new CancellationTokenSource();
public MainWindow() {
InitializeComponent();
Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs) {
KeyDown += OnKeyDown;
while (!cts.IsCancellationRequested) {
await Task.Delay(1000); // Some Long Task
tb.Text = (++_someVal).ToString();
}
}
private void OnKeyDown(object sender, KeyEventArgs keyEventArgs) {
if (keyEventArgs.Key == Key.A)
cts.Cancel();
}
}
It's just a rough demo, just take the concept. The only thing specific to WPF here is the manner of capturing the key-press. Everything else relating to breaking the while loop is the same across a console app or wpf or winforms.
You can create na event in the KeyDown Event in the MainWindow and get the KeyEventArgs e to know what key was pressed.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
// set a flag to break the loop
}
}
Now the cursor is focusing in the TextBox. If i click on the the Button (RemoveLostFocus),The TextBox's Lost focus event get fired. But What i need is , Lost Focus event of TextBox should not fire. is there any way to do so ?.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtUserName.Focus();
}
private void UserName_LostFocus(object sender, RoutedEventArgs e)
{
if (txtUserName.Text.Length < 1)
{
MessageBox.Show("UserName should not be empty");
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
anotherWindow.Show();
}
You will want to use the FocusManager attached properties to apply the focus to the TextBox when the Button focus changes
Example:
<StackPanel>
<TextBox Name="txtbx" />
<Button Content="Click Me!" FocusManager.FocusedElement="{Binding ElementName=txtbx}"/>
</StackPanel>
With this solution the The TextBox will always be focused even when the Button is pressed and the TextBox LostFocus event will not be fired
you could Set Focusable="False" on the Button. here is a link of the answer enter link description here
Set Focusable Property
<Button Focusable="False" />
In the Click-Event of you Button you can do something like
this.textBox.Focus();
If your lostfocus-method looks like this:
private void UserName_LostFocus(object sender, RoutedEventArgs e){ ... }
you can prevent the lostfocus with the following code:
this.textBox.LostFocus -= UserName_LostFocus;
In my application I used to use the wpf windows where are extendd from System.WIndows.Window.
I am thinking of migrate them to using Ribbon Windows which are extended From ToolWindow.
Unfortunately I can't use the OnClosing event with Ribbon windows.
How can I trigger when a window is closed?
I need something like the following
protected override void OnClosing(CancelEventArgs e) {
e.Cancel = true;
}
Thanks
Try this:
on XAML add Closing
<ribbon:RibbonWindow x:Class="RibbonWindowSample.RibbonWindowWord"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
xmlns:local="clr-namespace:RibbonWindowSample"
Title="RibbonWindowWord" Height="600" Width="1000"
Closing="RibbonWindow_Closing"
>
<TextBlock Text="Hello" />
</ribbon:RibbonWindow>
on Code add
private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Confirm?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
e.Cancel = true;
}
I have a UserControl in WPF. I also have a Borderless window. To move it- I use DragMove.
But- to get a click event in the user control- I use the PreviewMouseLeftButtonUp event and capture the mouse on UserControl_MouseEnter.
The problem is- that if I click the control, then move the window- the event can be triggered also when clicking near the control, not on it.
Here is my code:
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave">
<Grid>
</Grid>
</UserControl>
UserControl1.xaml.cs:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
CaptureMouse();
}
private void UserControl_MouseLeave(object sender, MouseEventArgs e)
{
ReleaseMouseCapture();
}
}
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1" WindowStyle="None" MouseLeftButtonDown="Window_MouseLeftButtonDown">
<Grid>
<my:UserControl1 Margin="39,29,380,199" Background="Red" PreviewMouseLeftButtonUp="UserControl1_PreviewMouseLeftButtonUp">
</my:UserControl1>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void UserControl1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Hello World");
}
}
If you run this app- you'll see that if you click on the control, then drag the window, then click near the control (the side may vary)- it will trigger the PreviewMouseLeftButtonUp event even though you didn't click on the control itself.
Any ideas how to solve this?
Thanks!
I asked around, and found that to solve the problem, I need to change the User control's events:
Instead of MouseLeave- I use MouseMove.
private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
if (this.IsEnabled)
CaptureMouse();
}
private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
Point mouseposition = e.GetPosition(this);
if (mouseposition.X < 0 || mouseposition.Y < 0 || mouseposition.X > this.ActualWidth || mouseposition.Y > this.ActualHeight)
ReleaseMouseCapture();
}