How can I handle the Paste event for a RichTextBox control in Silverlight 4? (I want to be able to copy-paste images - the Clipboard in SL4 supports only text, so I'm sending the ImageSource Uri, and on the Paste event I want to load the image in the RichTextBox instead of the Uri string).
public class MyRichTextBox : RichTextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
string text = Clipboard.GetText();
this.Selection.Text = text;
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
...
You can handle the Silverlight 4 clipboard events and then check if focus on the RichTextBox and then simply add the content as a paragraph or other such elements. Have a quick search for Silverlight 4+Clipboard on Google for some good examples.
You would need to handle checking the format of the clipboard text in your handler and then converting if necessary (e.g. plain text, text copied from another RichTextBox, HTML formnatted text etc).
Hope that helps,
Related
I am facing a problem with the text box control in WPF application.
The problem is that when the user selects multilines of the text and then clicks on tab, the selected lines are deleted instead of being indented to the right.
Is there a way to solve this issue?
Appreciate any help.
Thanks
Ahmad
You will need to handle it in the code behind as it is not the default action of a textbox. Many ways you can handle it. You will need to override the PreviewKeyDown and you can set the e.handled to true in order for text to not be overridden.
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox tbx = sender as TextBox;
if (e.Key == Key.Tab)
{
tbx.Text = tbx.Text.Insert(tbx.SelectionStart, "\t" + tbx.Text.Substring(tbx.SelectionStart));
e.Handled = true;
}
}
Sadly have to say that you have to implement that functionality, as the tab key was not made for that in the TextBox.
I've a text box in that i added a event TextChanged but it doesn't giving me all the data i entered in that text box it neglecting last character but i want all chars what i enter in the text box so how can i do this one please help me.
The TextChanged event gives you all the character:
TextBox currentTextBox = sender as TextBox;
string fullText = CurrentTextBox.Text;
Are you sure your not in like a PreviewKeyDownEvent or any other Preview Events. All of these events neglect the last input
Make an extended TextBox class and handle the KeyUp event to force an update to the text property binding after each key stroke.
public class MyWPFTextBox: TextBox
{
public MyWPFTextBox(): base()
{ KeyUp += new KeyEventHandler(MyWPFTextBox_KeyUp); }
private void MyWPFTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
BindingExpression BE = GetBindingExpression(TextBox.TextProperty);
if (BE != null)
BE.UpdateSource();
}
}
you should look at the PreviewTextInput event.
you also should take a look at mvvm pattern, binding and UpdateSourceTrigger=PropertyChanged. there you do not need events at all, you get your information via binding.
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.
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.
I have developed a small chat client using WPF. In each chat window, it contains a richtextbox to display previous chat conversations and a textbox with send button to type a chat message.
I want to format the display text in the richtextbox as shown below.
user1: chat message goes here
For the time being, I use AppendText function to append chat conversation to the richtextbox. my code looks like this,
this.ShowChatConversationsBox.AppendText(from+": "+text);
But in this way, i couldn't find a method to format the text as show above. Is there any way to do this? or any alternative methods?
thanks
Instead of interacting with the RichTextBox, you can interact with the FlowDocument directly to add rich text. Set the Document on the RichTextBox to a FlowDocument containing a Paragraph, and add Inline objects such as Run or Bold to the Paragraph. You can format the text by setting properties on the Paragraph or on the Inlines. For example:
public MainWindow()
{
InitializeComponent();
this.paragraph = new Paragraph();
this.ShowChatConversationsBox.Document = new FlowDocument(paragraph);
}
private Paragraph paragraph;
private void Button_Click(object sender, RoutedEventArgs e)
{
var from = "user1";
var text = "chat message goes here";
paragraph.Inlines.Add(new Bold(new Run(from + ": "))
{
Foreground = Brushes.Red
});
paragraph.Inlines.Add(text);
paragraph.Inlines.Add(new LineBreak());
}