Reading Keyboard Input from ViewModel - c#

i am using the MVVM pattern with WPF and Prism (unity). I have a tool which is reading a barcode scanned by a user and depending on what kind of barcode is scanned the tool is doing some stuff. Right now i have a textbox which is binded to a property. I would like to fill my property in the viewmodel with the content of the scanned barcode without using a textbox or similar. I would like to fill the property directly. Is there a way to do this? Or maybe someone have an idea how i could solve that problem?
kind regards

According to your comments, what you'll want to do is handle the keydown event.
Have a read here: code project scanner reader . He did what you want, and show what / where to handle :)
Edit:
I've answered another key events question that might be relevant. Feel free to have a look at my other answer , it discusses i:interaction and InputBindings , and points in return to another article about handling key events: up/down on datepicker, and discusses the code behind / mvvm approaches.
Hope you'll find them useful.

Well, I'm not sure I'm getting it in the right way, but you can catch all keyboard input with EventManager.
EventManager.RegisterClassHandler(typeof(Window),
Keyboard.KeyUpEvent,new KeyEventHandler(keyUp), true);
private void keyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.OemComma)
MessageBox.Show("Gotcha");
}
Another option is to create a read only textbox, but it's more or less same as you have now.

Related

How to enable ctrl+c shortcut in ComboBox (WindowsForm)

I am developing an app in C# with Visual Studio using Windows Forms
I have a ComboxBox and I would like to enable the Ctrl+C shortcut.
Right now it does not work (the selected text is simply not copied to the clipboard)
With a TextBox I simply need to set ShortcutEnabled to true, but this property does not seem to exist for ComboBox
Appearently it is what the guy in there tries to do but I don't undersand what he means.
MSDN does not elaborate much on how to do..
Shall I try to manually catch the shortcut in the
private void mycomboboxname_KeyDown(object sender, KeyEventArgs e)
method ?
That seems to me like an overkill... I looked here but did not understood it well (I can't find the IsEditable property when designing the form)
Any ideas welcome...
Hans Passant answer is right.
The key presses were simply intercepted (and prevented somewhere else in my code).

Windows Hub Template App Accessing Textboxes

I am working on a Windows Store Hub App using XAML and C#. I am not proficient in C#. I understand the basics. Please go easy on me. I know you're wondering why I would even attempt this and so am I at this point. I come from a web design background and could have used Javascript. But I have really learned a lot by doing it and googling what I didn't understand. The app controls really made me jump through hoops to do anything I wanted to do which must not be anything anyone else wants to do or there would be more documentation. I should also mention I've never worked very much with forms so I don't have that background either.
I actually do have a question. I am almost finished and on the last hub section. I want to have textboxes that accept user input. This will be for integers and will have a running total. I can write that code albeit primitively. What I can't do is access the textboxes inside the hub. If I try to put them outside the hub I can but then they don't scroll with the page so I have textboxes floating there.
If they weren't in the hub I could just write:
private void Button_Click(object sender, RoutedEventArgs e)
{
int a = Convert.ToInt32(tbOne.Text);
int b = Convert.ToInt32(tbTwo.Text);
int c = a + b;
Total.Text = c.ToString();
}
Of course, it will be more complicated than that but that is the basic idea. Inside the hub, it does not recognize the x:name.
I would appreciate any insight into this or links if anyone has any. I couldn't find anything by searching.
Investigate the MVVM pattern. Accessing all controls by name is not the best way to do this. You want to set the data context of the your Hub to a seperate class that has properties like TextBox1Text and TextBox2Text and that implements INotifyPropertyChanged. Then in XAML bind those to the controls like . This will give you access to those controls nested in the Hub.

WPF Context Sensitive Help - Best Practice

I have created a wpf application for context sensitive help using .chm files. I have created a a context sensitive help by pressing F1 after clicking a textbox or a button, thorugh xaml, AND NOT BY EVENT HANDLING. , but I need it for window also. I can have the window help as default in xaml when the window is loaded itself, which works now. But if i use text box help, then I cant switch back to window help since I have not included any events for that.
For this scenario, is using events the only possibility to include window help? what is the best practice? Is there a way to use window focus on xaml itself, or using event ends up as the best practice?
thanks a lot !!
Why not just implement a F1 Help System? it's pretty straightforward...
Take a look at this example HERE. This provides a HelpProvider class that gives you context sensitive help on any element that sets a HelpString. This should provide roughly what you need.
In your case, just make the chm file name your help name for each element you want a file sensitive to. And you could have something like so:
static private void Executed(object sender, ExecutedRoutedEventArgs e)
{
YourHelpSystem.ShowHelp(HelpProvider.GetHelpString(sender as FrameworkElement) + ".chm");
}

How to scroll a ListBox programmatically the same way the MouseWheelEvent does?

Ok, I have the following problem:
I would like to scroll an overflowing ListBox up and down.
I would like to do it programatically in a custom control inheriting from ListBox. I've seen and tested things such as scrollIntoView. However I would like to have a scrolling similar to what you can have when using the mouse's wheel.
I don't want to have the mouse involved at all (I'm developing for the Kinect, and since there are 2 cursors, I don't want to use the Mouse event args)
a google search didn't turn up much: I've read plenty of thread on how to scroll in code behind using scrollIntoView, or putting a scrollbar and such.
I think this will involve two steps:
Find the scrollViewer control inside listBox template
Perform the actual scrolling in that scrollViewer
For the first step implementation please take a look here. Here is the code snippet extracted from there:
this.Loaded += MainWindow_Loaded;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var scrollViewer = listbox.Template.FindName("Scroller", listbox);
}
And for the second step you should use one of the methods from here, LineDown or PageDown probably.
P.S.: I haven't tested this approach at all since I do not have VS installed so feel free to add the needed code here.

tabbed document interface in WPF using only on-board means?

I've seen two threads here about TDI & C#. Both of them didn't really answer the questions I have ...
Since TDIs are pretty much like a standard nowadays, I can hardly imagine, that I have to buy a special control (like AvalonDock or SandDock).
This must be possible with built in the tab-control(?) somehow! I don't need special features like dock- and draggable tabitems. Just open every form in a new tab. Thats it.
Like putting every forms content controls into user controls and by request (button, menu click ...) add a new tab and put the corresponding user control on it ... something like this.
How would you do it? This can't be THAT complicated (even for me) or am I missing something?!
thanks a lot!
Maybe Josh Smith's article on MVVM can give you an idea how to design such user interface. Example being built there is kinda tabbed document interface so you can use it as a starting block.
It's not that hard. It seems hard because there are a lot of different ways to do it.
Try this:
<TabControl x:Name="documentArea"/>
Handler for AddForm button:
private void AddFormClick(object sender, RoutedEventArgs e)
{
object form = GetNewForm();
documentArea.Items.Add(form);
}
That's it. You have to implement GetNewForm() in one of two ways. Have it return a user control that displays the form.
OR better yet, have it return your document that you want to display. Use a DataTemplate to select the controls to use for displaying this document. This method is going to be more complex to set up.

Categories

Resources