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
}
}
Related
I making a prompter, white text on black background scrolling to end. The errors received do not break the program, but they are suspicious.
I got:
Warning RichTextBox,
Name='RichPrompterTextBox' Text.Length TextBlock.Text,
Name='CharacterCounterTextBlock' String Text property not found on
object of type RichTextBox. Warning RichTextBox,
Name='RichPrompterTextBox' MaxLength TextBlock.Text,
Name='CharacterCounterTextBlock' String MaxLength property not found
on object of type RichTextBox. Error null (0) TextBlock.Visibility,
Name='CharacterCounterTextBlock' Visibility Cannot find source:
RelativeSource FindAncestor,
AncestorType='System.Windows.Controls.TextBox', AncestorLevel='1'.
I have this XAML:
<Window x:Class="WpfDrawing.Prompter.PrompterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDrawing.View"
xmlns:prompter="clr-namespace:WpfDrawing.Prompter" d:DataContext="{d:DesignInstance Type=prompter:PrompterViewModel}"
mc:Ignorable="d"
WindowState="Maximized"
WindowStyle="None"
Title="Prompter" Height="450" Width="800">
<RichTextBox
Background="Black"
Name ="prompterRichTextBox"
Foreground="White">
</RichTextBox>
</Window>
and this .cs:
/// <summary>
/// Interaction logic for Prompter.xaml
/// </summary>
public partial class PrompterView : Window
{
private readonly PrompterViewModel vm;
public PrompterView(PrompterViewModel vm)
{
this.vm = vm;
DataContext = vm;
InitializeComponent();
this.vm.PropertyChanged += Vm_PropertyChanged;
this.RichPrompterTextBox.MouseWheel += RichPrompterTextBox_MouseWheel;
this.RichPrompterTextBox.LayoutUpdated += RichPrompterTextBox_LayoutUpdated;
this.RichPrompterTextBox.TextChanged += RichPrompterTextBox_TextChanged;
SetToAdditionalMonitor();
}
private void SetToAdditionalMonitor()
{
var screens = Screen.AllScreens;
for (var i = 0; i != screens.Length; i++)
{
if (!screens[i].Primary)
{
this.WindowState = WindowState.Normal;
this.WindowStartupLocation = WindowStartupLocation.Manual;
this.Left = screens[i].Bounds.Left;
this.Top = screens[i].Bounds.Top;
this.Width = screens[i].Bounds.Width;
this.Height = screens[i].Bounds.Height;
}
}
}
private void RichPrompterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var textRange = new TextRange(this.RichPrompterTextBox.Document.ContentStart, this.RichPrompterTextBox.Document.ContentEnd);
vm.Text = textRange.Text;
}
private void RichPrompterTextBox_LayoutUpdated(object sender, System.EventArgs e)
{
vm.Height = this.RichPrompterTextBox.ViewportHeight + this.RichPrompterTextBox.ExtentHeight;
}
private void RichPrompterTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
vm.ScrollPosition = RichPrompterTextBox.VerticalOffset;
}
private void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(vm.ScrollPosition))
{
this.RichPrompterTextBox.Dispatcher.Invoke(() => this.RichPrompterTextBox.ScrollToVerticalOffset(vm.ScrollPosition));
}
if(e.PropertyName == nameof(vm.Text))
{
var textRange = new TextRange(this.RichPrompterTextBox.Document.ContentStart, this.RichPrompterTextBox.Document.ContentEnd);
if (textRange.Text != vm.Text)
{
this.RichPrompterTextBox.Document = new FlowDocument(new Paragraph(new Run(vm.Text)));
}
}
}
private void Reset(object sender, RoutedEventArgs e)
{
vm.Reset();
}
private void StartStop(object sender, RoutedEventArgs e)
{
vm.RunStop();
}
private void CloseWindow(object sender, RoutedEventArgs e) => Close();
}
Xaml errors:
errors
I didn't bind anything, but I get binding errors.
These errors are not related to the code you've posted.
In your program you have bindings because these errors are indeed binding errors, where it seems that:
you're binding the Text property of the instance CharacterCounterTextBlock (of type TextBlock ) to a property named Text.Length of an instance of type RichTextBox named prompterRichTextBox. But the type RichTextBox doesn't have that propery.
you're binding the Text property of the instance CharacterCounterTextBlock (of type TextBlock ) to a property named MaxLength of an instance of type RichTextBox named prompterRichTextBox. But the type RichTextBox doesn't have that propery.
you're binding the Visibility property of the instance CharacterCounterTextBlock (of type TextBlock ) to something that's null because is not found in the source declaration.
More generally speaking, you shouldn't ask questions to solve specific issues related to bugs in your code, but if it's the case, please try to replicate the issue in a GitHub project (or similar public repositories), so that it's easier to understand the code you've used and find the cause.
I'm stuck with wpf commands and "non-trivial" hotkeys. I want to map "+" key to some command. Within i want it to keep working with any textbox. Below is the sample
Commands.cs
public static class Commands
{
private static readonly ICommand _someCommand;
static Commands()
{
_someCommand = new RoutedCommand("cmd", typeof(Commands), new InputGestureCollection { new KeyGesture(Key.OemPlus), new KeyGesture(Key.Add) });
}
public static ICommand SomeCommand
{
get { return _someCommand; }
}
}
MainWindow.xaml
<Window x:Class="WpfHotkeysTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfHotkeysTest="clr-namespace:WpfHotkeysTest"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="wpfHotkeysTest:Commands.SomeCommand" Executed="CommandBinding_OnExecuted"></CommandBinding>
</Window.CommandBindings>
<TextBox></TextBox>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("COMMAND! " + e.Source);
}
}
The problem is when im focused on the textbox it does not handle pressed "+" key before command is executed.
I want my key be displayed, but how do i achieve this in the best way?
UPD
I dont want to execute command if the key was handled by the text box.
I know there is property CanExecuteRoutingEventArgs.ContinueRouting. But it executes both command and textbox handling
The workaround I've found so far
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
Debug.WriteLine("COMMAND! " + e.Source);
}
private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (IsEditableControlSelected())
{
e.ContinueRouting = true;
return;
}
e.CanExecute = true;
}
private bool IsEditableControlSelected()
{
return Keyboard.FocusedElement is TextBox;
}
}
This brings tears to my eyes, but at least this is better than nothing. Waiting for more solutions
I have two xaml. One is MainWindow and other is NewWindow.
I want show NewWindow 5 seconds, when program is run.
And after 5 seconds, I want show MainWindow.
How to change xaml in WPF?
Here is MainWindow.
<Window x:Class="WpfApplication2.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">
<Grid>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Here is NewWindow.
<Window x:Class="WpfApplication2.NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NewWindow" Height="300" Width="300">
<Grid>
</Grid>
public partial class NewWindow : Window
{
public NewWindow()
{
InitializeComponent();
}
}
1) First, we need to stop MainWindow from opening as soon as we run the Application. To do this, first remove the StartupUri="MainWindow.xaml" setting from the App.xaml file and replace it by setting the Startup property instead:
<Application x:Class="AppName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
2) Then, add the handler for the Application.Startup event and launch your child (or splash screen) Window:
private SplashScreen splashScreen;
...
public void App_Startup(object sender, StartupEventArgs e)
{
// Open your child Window here
splashScreen = new SplashScreen();
splashScreen.Show();
}
3) At this point, there are several different ways to go, dependent on whether you need to wait for the SplashScreen Window to do anything or not. In this particular question, the requirement is to simply open the MainWindow after 5 seconds, so we'll need a DispatcherTimer:
public void App_Startup(object sender, StartupEventArgs e)
{
// Open your child Window here
splashScreen = new SplashScreen();
splashScreen.Show();
// Initialise timer
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 5, 0);
timer.Tick += Timer_Tick;
}
...
private void Timer_Tick(object sender, EventArgs e)
{
splashScreen.Close();
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
That's it.
There are plenty ways to do this. As some people suggested i suggest too to DO NOT DO THIS if you're trying to create a splash screen, there are better ways to do that. But.. here what you asked for:
using System.ComponentModel; //Remember to add this
public partial class MainWindow : Window
{
private BackgroundWorker waitingWorker = new BackgroundWorker();
private NewWindow myNewWindow = new NewWindow();
public MainWindow()
{
InitializeComponent();
waitingWorker.DoWork += waitingWorker_DoWork;
waitingWorker.RunWorkerCompleted += waitingWorker_RunWorkerCompleted;
waitingWorker.RunWorkerAsync();
}
private void waitingWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
myNewWindow.Show();
this.Close();
}
private void waitingWorker_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(5000);
}
}
it's a simple background worker that waits for 5 seconds, then opens the NewWindow and close MainWindow. Yes, you can do it without background worker too, but Thread.Sleep(5000); will totally freeze your GUI and make your little app unresponsive, so you need another thread to wait while the main thread can keep your GUI alive. I suggest you to study at least how a background worker works.
HERE the official MSDN documentation, but google is your friend and you can find tons of tutorial and explanation about it
Here is another way to do it:
Set Startup to "App_Startup" as shown in one of the other posts.
<Application x:Class="AppName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
And in App_OnStartup:
private async void App_Startup(object sender, StartupEventArgs e)
{
var splash = new SplashWindow();
splash.Show();
await Task.Delay(5000);
var mainWindow = new MainWindow();
mainWindow.Show();
splash.Close();
}
The mainWindow should also be loaded before closing the splashScreen This way your splashscreen shows as long as it is loading.You can add additional time in the splashScreen.Close() Function.
private SplashScreen splashScreen;
private void App_Startup(object sender, StartupEventArgs e)
{
splashScreen = new SplashScreen("SplashScreen1.png"); // Or new WPF window
splashScreen.Show(false);
MainWindow mainWindow = new MainWindow();
splashScreen.Close(new TimeSpan(0, 0, 3));
mainWindow.Show();
}
In I have created a control that has a text box and a text changed event handler attached to it - this is in xaml.
The problem: when control is loaded the text changed event is fired, I do not want it to happen when the control is loaded only when I make actually make it change on the control by typing something.
What do you pros suggest I do? :)
All you have to do is check the textbox's IsLoaded property inside the event handler before handling it.
Attach Your EventHandler after the InitializeComponent Method in your constructor not in the Xaml.
i.e.
public MainWindow()
{
InitializeComponent();
textBox1.TextChanged+=new TextChangedEventHandler(textBox1_TextChanged);
}
I noticed that you are talking about an usercontrol, the only thing I can think of off the top of my head is to to create a property that can be used to inhibit the TextChanged Event until the Parent Form finishes loading. See if something like this works.
MainForm Xaml:
<my:UserControl1 setInhibit="True" HorizontalAlignment="Left" Margin="111,103,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="55" Width="149" setText="Hello" />
MainForm CS
private void Window_Loaded(object sender, RoutedEventArgs e)
{
userControl11.setInhibit = false;
}
UserControl:
public UserControl1()
{
InitializeComponent();
textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
}
public string setText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public bool setInhibit { get; set; }
void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (setInhibit) return;
// Do your work here
}
UserControl1.xaml:
<Grid>
<TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged"/>
</Grid>
where TextChanged is the original event for TextBox
UserControl1.xaml.cs:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
_isFirstTime = true;
DataContext = this;
InitializeComponent();
}
public event TextChangedEventHandler TextBoxTextChanged;
bool _isFirstTime;
//MyText Dependency Property
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
public static readonly DependencyProperty MyTextProperty =
DependencyProperty.Register("MyText", typeof(string), typeof(UserControl1), new UIPropertyMetadata(""));
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (TextBoxTextChanged != null)
if (!_isFirstTime)
{
TextBoxTextChanged(sender, e);
}
_isFirstTime = false;
}
}
where TextBox_TextChanged is the customized eventHandler for original TextChanged
and TextBoxTextChanged is more like a wrapper for the original TextChanged
Window.xaml:
<Grid>
<c:UserControl1 TextBoxTextChanged="TextBoxValueChanged"/>
</Grid>
as you see you can add an eventHandler to the event wrapper (TextBoxTextChanged)
Window.xaml.cs:
private void TextBoxValueChanged(object sender, TextChangedEventArgs e)
{
MessageBox.Show("asd");
}
finally TextBoxValueChanged won't be fired the first time Text is changed
private void TextBoxValueChanged(object sender, TextChangedEventArgs e)
{
if (Textbox1.IsFocused)
{
App.Current.Properties["TextChanged"] = "1"; // Set Flag
}
}
private void TextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (App.Current.Properties["TextChanged"] == "1")
{
// Do Your Wor Here
App.Current.Properties["TextChanged"] = "0"; // Clear Flag
}
}
On your XAML:
<TextBox xName="TextBox1" LostFocus="TextBoxLostFocus" TextChanged="TextBoxValueChanged"/>
(This is a very rudimentary, dirty, codebehind hack... checking the IsLoaded property as stated by Brent I found to be efficient)
Here since on textbox control creation it's not focused, the TextChanged event will fire but the flag "1" is NOT set...
Later when user leaves field after editing it, since it had focus the Flag is set... the LostFocus is fired, but only runnig code if textbox was changed.
I found a way of preventing this behavior across multiple inputs without having to create a unique bool for each input...
private void TextChanged_UpdateItem(object sender, TextChangedEventArg e)
{
TextBox txtBox = sender as TextBox;
if (!txtBox.IsFocused)
return;
//The rest of your code here
}
So basically, if the text field doesn't have focus (like on initialization) it just returns. This also prevents it from firing if the data is changed elsewhere. :)
Alternatively, as mentioned by Brent, you can just look for "IsLoaded":
private void TextChanged_UpdateItem(object sender, TextChangedEventArg e)
{
TextBox txtBox = sender as TextBox;
if (!txtBox.IsLoaded)
return;
//The rest of your code here
}
Working on a TouchScreen application which also has a keyboard attached, I have the following problem:
The WPF window has a TextBox, which should receive ALL keyboard input. There are also Buttons and a ListBox, which are solely used by the TouchScreen(=Mouse).
A very simple example looks like this:
<Window x:Class="KeyboardFocusTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<StackPanel>
<TextBox Text="{Binding Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus"/>
<Button Click="Button_Click">Add</Button>
<ListBox ItemsSource="{Binding Strings}" />
</StackPanel>
</Window>
To keep the TextBox always focused, I just do:
private void TextBox_PreviewLostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}
So far so good - the problem now is, that I can't select items from the ListBox anymore. This only seems to work, if the ListBox has the keyboard focus. But if I loose the keyboard focus on the TextBox, I can't enter text anymore without clicking it first.
Any ideas, comments suggestions are welcome!
There might be a more elegant solution for this, but you could always handle the PreviewKeyDown event at the Window level, and pass focus to the TextBox if it doesn't already have it, instead of preventing it from losing focus in the first place. That way, the ListBox can use focus as is normal, but as soon as a key is pressed focus jumps right to the TextBox. In addition, you can filter out keys that you don't want to switch focus - the arrow keys come to mind, which could then be used to move up and down in the ListBox.
Adding an event handler like the following should do the trick:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!textBox.IsFocused)
{
textBox.Focus();
}
}
Based on Nicholas' suggestion (thx!), here's a markup extension, which is used like:
<TextBox Helpers:KeyboardFocusAttractor.IsAttracted="true" />
It seems to work, and ANTS didn't show any memory leaks. But when it comes to WPF and especially events and bindings, you never know, so use with care!
public static class KeyboardFocusAttractor
{
public static readonly DependencyProperty IsAttracted = DependencyProperty.RegisterAttached("IsAttracted",
typeof (bool), typeof (KeyboardFocusAttractor), new PropertyMetadata(false, OnIsAttracted));
private static void OnIsAttracted(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var isAttracted = (bool) e.NewValue;
var controlWithInputFocus = d as Control;
if (controlWithInputFocus != null)
{
if (isAttracted)
{
new KeyboardInputFocusEventManager(controlWithInputFocus);
}
}
}
public static void SetIsAttracted(DependencyObject dp, bool value)
{
dp.SetValue(IsAttracted, value);
}
public static bool GetIsAttracted(DependencyObject dp)
{
return (bool) dp.GetValue(IsAttracted);
}
private class KeyboardInputFocusEventManager
{
private readonly Control _control;
private Window _window;
public KeyboardInputFocusEventManager(Control control)
{
_control = control;
_control.Loaded += ControlLoaded;
_control.IsVisibleChanged += ControlIsVisibleChanged;
_control.Unloaded += ControlUnloaded;
}
private void ControlLoaded(object sender, RoutedEventArgs e)
{
_window = Window.GetWindow(_control);
if (_window != null)
{
_control.Unloaded += ControlUnloaded;
_control.IsVisibleChanged += ControlIsVisibleChanged;
if (_control.IsVisible)
{
_window.PreviewKeyDown += ParentWindowPreviewKeyDown;
}
}
}
private void ControlUnloaded(object sender, RoutedEventArgs e)
{
_control.Unloaded -= ControlUnloaded;
_control.IsVisibleChanged -= ControlIsVisibleChanged;
}
private void ControlIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_window != null)
{
_window.PreviewKeyDown -= ParentWindowPreviewKeyDown;
}
if (_control.IsVisible)
{
_window = Window.GetWindow(_control);
if (_window != null)
{
_window.PreviewKeyDown += ParentWindowPreviewKeyDown;
}
}
}
private void ParentWindowPreviewKeyDown(object sender, KeyEventArgs e)
{
Keyboard.Focus(_control);
}
}
}