SL 4: TextBox doesn't get Ctrl-V? - silverlight-4.0

We have a TextBox control as part of a larger UserControl.
The UserControl has an MVVMLight RelayCommand bound to the KeyDown event:
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding KeyDownCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger> </i:Interaction.Trigger>
When the TextBox has focus, the KeyDown events for the Ctrl key are seen, but not the Ctrl+V.
I added a handler for the KeyDown for the TextBox in code-behind:
KeyDown="textBox_KeyDown"
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("textBox_KeyDown Key: {0} Modifier: {1}", e.Key, Keyboard.Modifiers));
e.Handled = false;
}
Similar result -- a KeyDown event for the Ctrl key is handled, but the Ctrl+V doesn't show up.
The message bubbles up through other handlers, but is never marked handled.
Any ideas on why the TextBox isn't getting the Ctrl+V?
Thanks for any insights.
Update: Oh, for Pete's sake... I added a KeyUp handler in the code-behind, and the Ctrl+V appears there. However, the Ctrl+V appears in a KeyDown handler when a different control (a containing ScrollViewer, for example) is the source of the KeyDown.
Update the Second: With a new, sparse project, I verified that the TextBox.KeyDown handler does NOT get called for Ctrl+V, while TextBox.KeyUp gets called for Ctrl+V. Is it possible to "pass along" the Ctrl+V event to another TextBox?

Related

How to prevent Esc key from light dismissing a popup?

I have a Popup which needs to be light dismissed when user taps an element outside the popup. By default, pressing the Esc key also triggers LightDismiss, which I would like to prevent. Instead, I'd like to handle this KeyDown event explicitly.
Handling the Tapped event in the page's root to check if user's tapped outside the popup is one solution, but since every tap in my app will go through this, I'd prefer not to take this approach.
Some pseudo-code:
<Page>
<Grid>
<Popup
x:Name="FooTip"
Opened="FooTip_Opened"
Closed="FooTip_Closed"
IsLightDismissEnabled="True"
ShouldConstrainToRootBounds="True">
<!-- Popup content here -->
</Popup>
</Grid>
</Page>
TLDR: FooTip should not get light dismissed when Esc key is pressed.
FooTip should not get light dismissed when Esc key is pressed.
You could handle the PreviewKeyDown event for the Page like this:
private void OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape
&& FooTip.IsOpen)
{
e.Handled = true;
}
}

Reset SelectedItem in LongListSelector after selection

I'm using Caliburn.Micro and the LongListSelector. Because binding SelectedItem is a problem I act on the SelectionChanged event. Problem is, after returning to the list, when I click the same item again, it is already selected. So the event doesn't fire. I could set the SelectedIndex to -1 or something, but in Caliburn.Micro I can't access UI controls. That's the point of MVVM, isn't it?! :)
Here's my XAML
<LongListSelector x:Name="NewsItems"
ItemsSource="{Binding NewsItems}"
cal:Message.Attach="[Event SelectionChanged] = [NavigateToArticle($eventArgs)]" />
How to solve this? How can I reset the SelectedItem when I can't access the LongListSelector from code?
Thanks!
You have to put your LongListSelector SelctionMode="Multiple",
or
You can get it using Gesture Tap event.
Did not understood real problem but
I think you can solve your problem if selection change event fire every time when user select item-
private void productList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector productList= (LongListSelector)sender;
if (productList.SelectedItem == null)
return;
//Write your code here
//For Tapping many times..
productList.SelectedItem = null;
}
The above code will make selection change event to fire on selection of same item every time.

How to bind event in the style in Silverlight 4

I have a control where is about 100 hundred TextBoxes, and I need for each of them have binded event to GotFocus event (where I select all text).
I cant use EventSetter as in WPF, so what do you use to bind event in style?
You'll have to subclass the TextBox class and then use that in all your code.
You can then put the GotFocus event handler in that subclass, otherwise you'd have to add the GotFocus event handler to all your code.
public class MyTextBox : TextBox
{
protected override void OnGotFocus(RoutedEventArgs e)
{
// Add your code in here
base.OnGotFocus(e);
}
}
Then in your XAML you'd have:
<my:MyTextBox ..... />

Why isn't the LostFocus event occurring?

With a listbox visible, I have clicked on the windows form hoping to use the listbox.lostfocus event to let me hide the listbox - but the event does not occur. I suppose I can use the form.click event to hide the listbox, but how would I get the form to accept focus?
A Form does not want to receive the focus. It was designed to be a container control, it makes sure that one of its child controls always gets the focus. It is technically possible to whack it over the head and make it lose that behavior:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.SetStyle(ControlStyles.ContainerControl, false);
}
protected override void OnClick(EventArgs e) {
this.Focus();
base.OnClick(e);
}
}
This is however a bad idea. A Form doesn't have any way to indicate that it has the focus, you'll also have to override OnPaint() to do something like draw a focus rectangle. If you don't then the user completely loses track of where the focus is located. Then there's the considerable inconvenience that nothing interesting can happen when the user uses the keyboard, a form doesn't have a use for it.
Don't do this. If you want to make a control disappear then add a menu item, toolbar button or a normal button to your UI. Something the user can click on.
the LostFocus event work when the focus move to another control like textbox,... or when the form all of it lost the focus you can use click event for the form to detect taht

How to force LostFocus when button is clicked

XAML C# not WEB page is a Window
At the click of a button I:
Need to trap the name of the last control OnFocus
Force the LostFocus event of the control.
// PROBLEM: clicking on btns does not force lostfocus event on the last entered element control (last entry control could be text,checkbox or others)
added save button, where calling such method it moves focus to parent forcing lostfocus on last element.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
AcceptLastFocusedElement(sender, e);
}
private void AcceptLastFocusedElement(object sender, RoutedEventArgs e)
{
FocusManager.SetFocusedElement(this, (Button)sender);
}
NOTE: no need for task number 1 (getting the name of the element).
You could make use of the LayoutUpdated method.
So whenever there is any event happening, it goes into the LayoutUpdatedevent and you could trap the LastFocusObject .