I have 2 RichTextBoxes (rtb1, rtb2), I something wrote in rtb1 and click on enter key, on this event is added text from rtb1 to rtb2. I solved this in code behind, it is possible this same write in XAML?
C# code:
private void rtb2_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var textElement = new Run() { Text = rtb2.Text };
var paragraph = new Paragraph();
paragraph.Inlines.Add(textElement);
rtb1.Document.Blocks.Add(paragraph);
rtb2.Document.Blocks.Clear();
//On this place I would like set start position for input text in rtb2 richtextbox on the start position
}
}
Thank for your advances.
If you really need your combined RichTextBox to have generalized document editing capabilities, what you are doing is probably the easiest and no, there is no pure-XAML way to do it.
On the other hand, if you are writing a chat application or something like it there may be a much better way to do it: Replace your combined RichTextBox with an ItemsControl that displays all the chat items as individual items. Then when Enter is pressed, add the text to a collection in your model. As long as the collection implements INotifyCollectionChanged, the new text will appear in your ItemsControl. And if the template you use for your ItemsControl allows editing, your user will be able to edit the item.
Which way you go all depends on what you're trying to accomplish.
Related
This is a similar question to this one, but I'm hoping that a better answer has come along in the six years since it was asked.
I have a custom dictionary that I want to use in all textboxes in the application. I don't seem to be able to set the SpellCheck.CustomDictionaries property in a style, like I can with the SpellCheck.IsEnabled property, and I don't want to have to add the setter to every textbox individually.
The answer posted in that question requires hooking in to the Loaded event of every window where you want to use the custom dictionary. We have a large application that is growing all the time, and do not want to have to add handlers for every window, as well as rely on developers remembering to add the code when they add a new window.
Is there any better way to specify a custom dictionary for the whole application? Thanks.
Probably not what you wanted, but I used this EventManager.RegisterClassHandler to handle all of certain type's certain event.
For ex, here I am registering to all TextBoxes' LoadedEvent in MainWindow's constructor:
EventManager.RegisterClassHandler(typeof(TextBox), FrameworkElement.LoadedEvent, new RoutedEventHandler(SetCustomDictionary), true);
and define the RoutedEventHandler:
private void SetCustomDictionary(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
textBox.SpellCheck.IsEnabled = true;
Uri uri = new Uri("pack://application:,,,/MyCustom.lex");
if (!textBox.SpellCheck.CustomDictionaries.Contains(uri))
textBox.SpellCheck.CustomDictionaries.Add(uri);
}
}
I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}
Originally I added to the input bindings of the actual treeviewitem on the loaded event like this:
void MultiSelectTreeViewItem_Loaded(object sender, RoutedEventArgs e)
{
this.InputBindings.Add(new KeyBinding(ApplicationCommands.SelectAll, new KeyGesture(Key.A, ModifierKeys.Control)));
SignalViewModel svm = this.DataContext as SignalViewModel;
if (svm != null)
{
this.InputBindings.Add(new KeyBinding(DaedalusGraphViewer.GraphViewer.AddSignalsCommand, new KeyGesture(Key.Enter)));
}
BitViewModel bvm = this.DataContext as BitViewModel;
if (bvm != null)
{
this.InputBindings.Add(new KeyBinding(DaedalusGraphViewer.GraphViewer.OpenNewBusDialogCommand, new KeyGesture(Key.Enter)));
}
}
the problem with this is that if I want to use the treeviewitem elsewhere and have different commands, I would have to make a new treeviewitem class each time. I am definitely violating MVVM here and coupling too much so I want to fix this old code (been trying to adopt mvvm practices lately, but there is still a lot of old stuff that I didn't do correctly).
What I would like is a way to attach the keybinding in xaml, but I am still a little hazy on what determines whether a keybinding works. is a keybinding only available if the item on which the keybinding exists is in focus? can you access parent keybindings or child keybindings of the currently focused item? Based on my testing it looked like you can't access keybindings from the parent and I read another stackoverflow post that made it seem like you can't access children either. Is that the case?
I just need a nice customizeable way to set keybindings on different tiers of a treeview that is templateable so that I don't have to make new classes every time.
edit:
it occurs to me that one possible solution is to attach the input bindings to the textbox at each tier with a different command for each, then stretch the textbox to fill the item and put the focus on the textbox instead of the treeview. is that the best option? ideally I kind of wish hierarchicaldatatemplate had input bindings or something.
Could anyone tell me which controls are used in WP7 / WP7.5 where we go on e-mails reading, and when we click on the "search" button?
It appears on the top of a searchbox (a TextBox) and the middlle/bottom of the page is a little blurred. I need to code an application like this, and I don"t know which control to use.
Thanks a lot.
Best regards
There's no one control that does the exact scenario you described.
You'll have to add a Textbox and a list for search on the bottom by yourself. Then, respond when the user hits the the "Enter" key, like so:
XAML:
<TextBox Name="textBox1" KeyDown="OnKeyDownHandler"/>
C#:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Search the data source you want
}
}
You can use a TextBox and a list box to show the searched result. If you want you can use an image button to show search icon. You can use this button's click event to perform the search operation and can show the result in the listbox below.
How do I make it so that when a user types letters into a textbox in silverlight that it changes these letters to all capitals? And at the same time still fires all the same events such as keypress.
If you want to avoid code behind or custom controls (preferable), this sort of functionality is available via behaviours.
e.g. this one in the Expression blend Gallery converts any text box to which it is attached into uppercase.
Then is just a matter of drag-drop in Blend or adding the trigger manually in VS2010.
There was no easy way to do it except to use an embedded all caps font. All other ways interfered with how the textbox worked.
The simplest way would be:
private void tb_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = (TextBox)sender;
var caret = tb.CaretIndex;
tb.Text = tb.Text.ToUpper();
tb.CaretIndex = caret;
}
If you wanna save even more performance, handle the LostFocus event instead (which you then don't have to worry about carret position either):
private void tb_LostFocus(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
tb.Text = tb.Text.ToUpper();
}
But if the data is bound to a model/entity I would call the ToUpper() on the bound object property setter / OnPropertyChanged, OnTextChanged or whatever it is in the entity.