WPF Paste event on whole application/window [duplicate] - wpf

I'm developing a WPF application, and I'd like to catch the paste command in a RichTextBox input and handle the pasted files. For this I'm using the following callback:
<RichTextBox DataObject.Pasting="BodyRichTextBox_Pasting">
private void BodyRichTextBox_Pasting(object sender, DataObjectPastingEventArgs e) {
// handler 1
}
I can handle the text and image pasting, but when I'd like to paste files, then this callback doesn't get fired.
I also can catch the paste event with:
<RichTextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Executed" />
</RichTextBox.CommandBindings>
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
// handler 2
}
But this callback overrides the previous one, and I need important fields from the DataObjectPastingEventArgs variable. So I wanted to use the first handler to handle text and image pasting and the second for file pasting, but doesn't works both together (If I set e.Handled to false doesn't help).
Any other idea how to do this?

You could handle the ApplicationCommands.Paste command something like this to get the pasted in text, file(s) or image:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ApplicationCommands.Paste)
{
var files = Clipboard.GetFileDropList();
var text = Clipboard.GetData(DataFormats.Text);
var image = Clipboard.GetImage();
e.Handled = true;
}
}

Related

WPF handling text, image and file pasting event

I'm developing a WPF application, and I'd like to catch the paste command in a RichTextBox input and handle the pasted files. For this I'm using the following callback:
<RichTextBox DataObject.Pasting="BodyRichTextBox_Pasting">
private void BodyRichTextBox_Pasting(object sender, DataObjectPastingEventArgs e) {
// handler 1
}
I can handle the text and image pasting, but when I'd like to paste files, then this callback doesn't get fired.
I also can catch the paste event with:
<RichTextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Executed" />
</RichTextBox.CommandBindings>
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
// handler 2
}
But this callback overrides the previous one, and I need important fields from the DataObjectPastingEventArgs variable. So I wanted to use the first handler to handle text and image pasting and the second for file pasting, but doesn't works both together (If I set e.Handled to false doesn't help).
Any other idea how to do this?
You could handle the ApplicationCommands.Paste command something like this to get the pasted in text, file(s) or image:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ApplicationCommands.Paste)
{
var files = Clipboard.GetFileDropList();
var text = Clipboard.GetData(DataFormats.Text);
var image = Clipboard.GetImage();
e.Handled = true;
}
}

It is possible to create "new MouseButtonEventArgs" in Silverlight?

I'm newbie in Silverlight and I need to activate MouseRightButtonDown(object sender, MouseButtonEventArgs e) from an another event handler in my application.
I have found, that in WPF it is possible to do somthing like this:
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
MouseButtonEventArgs args = new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice,
0,
MouseButton.Right);
MouseRightButtonDown(sender, args);
}
But I have in Silverlight neither InputManager-Class nor MouseButton-Class... It is generally possible to realise something like that?
I want to do it, because I try to select an DataGridRow(within an custom control) with help of right-mouse-button. Without context menu it is easily possible, but when I switch context menu on, then context menu opens and event will not fired...
My code snippet:
public override void OnApplyTemplate() {
DataGrid = (DataGrid)GetTemplateChild("DataGrid");
DataGrid.MouseRightButtonDown += DataGridMouseRightButtonDown;
ContextMenu = (ContextMenu)GetTemplateChild("ContextMenu");
ContextMenu.Opened += OnContextMenuOpened;
}
private void DataGridMouseRightButtonDown(object sender, MouseButtonEventArgs e) {
//My code to select an DataGridRow
}
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
//This event-handler now will be always activated if I do
//right-mouse-button-click
}
Thanks a lot for help!
The Results of my research has shown, that it is impossible in silverlight -.-

What event can I change the caret in in a winforms maskedtextbox

Right now, I'm calling the win32 createcaret/showcaret in the keypress event of my masked textbox. That changes it fine. I want the caret to change when the box is entered, though, either by tab or by click.
Unfortunately the enter event or even the invalidate event aren't suitable places to change that caret. It doesn't change, maybe because they fire too early.
So anyway, how can I get the caret to change on textbox enter without handling it in the enter event?
You need to add DestroyCaret to your routine, too:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_GotFocus(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 6, textBox1.Height);
ShowCaret(textBox1.Handle);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
DestroyCaret();
}

Silverlight Button Click

I'm having problems getting the Silverlight Button Click event to immediately update a control's UI element and then continue doing some other process. For example, update the text of a control and then do some process. I've tried calling the UpdateLayout() method but that doesn't help.
Here is some sample code:
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Testing";
textBlock1.UpdateLayout();
UpdateLayout();
Thread.Sleep(2000);
textBlock1.Text = "Done";
}
In that sample, the textBlock1 control will never display the text "Testing".
It's because you've blocked the UI thread with your Sleep statement which means that the text block won't update until after it's completed, but by that time you've set the text to "Done".
This doesn't work. The UI will block when you do not exit the method. This is why in Silverlight you usually do everything asynchronously:
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "Testing";
var myTask = /* ... */
myTask.Completed += new FancyDelegate(myTask_Completed);
}
private void myTask_Completed(object sender, RoutetEventArgs e)
{
textBlock1.Text = "Done.";
}
If your tasks do not have asynchronous functions, just wrap them. But be reminded, that when you want to change your textBlock1.Text property from another thread, you must invoke it with the Dispatcher.

How can I disable the default RichTextBox command for Ctrl+1?

Snoop shows that the command is "ApplySingleSpace", but when I try disabling it via the method described in this article . Like this:
<RichTextBox.CommandBindings>
<CommandBinding
Command="ApplySingleSpace"
CanExecute="BlockTheCommand"/>
</RichTextBox.CommandBindings>
.
private void BlockTheCommand(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
My app crashes because there is no ApplySingleSpace command. ApplySingleSpace is not in the EditingCommands either.
What am I missing?
Unfortunately that will not work for
me. The reason I am trying to disable
the command is that I have a
KeyBinding in a higher nested view
that is not firing because the CTRL+1
gesture is being swallowed by the
richtextbox which has keyboardfocus.
How about overwriting that KeyBinding with a custom command that does what you want instead of trying to somehow disable it?
<RichTextBox.InputBindings>
<KeyBinding Command="local:YourCommands.Cmd1" Gesture="CTRL+1" />
<RichTextBox.InputBindings>
Taken from this question.
Using the code from this answer
How can I programmatically generate keypress events in C#?
to refire all events on PreviewKeyDown other than those you want handled by the richtextbox seems to work for me. (I only need Ctrl-C for copy). Of course you could make it so it only refires Ctrl-1 if that's what you need.
private void logKeyHandler(object sender, KeyEventArgs e)
{
if (!(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C))
{
e.Handled = true;
var routedEvent = Keyboard.KeyDownEvent;
this.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromDependencyObject(this),
0,
e.Key) { RoutedEvent = routedEvent }
);
}
}
What about trying with the gesture instead...
<RichTextBox.InputBindings>
<KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
</RichTextBox.InputBindings>

Resources