WPF: Mystery with RichTextBox and KeyDown Event - wpf

Before I explain, I will present code, because it is the best way to express the question in this case. a simple WPF window, his base node contains this:
<StackPanel>
<Menu>
<TextBox x:Name"A" Width="200" />
</Menu>
<TextBlock Margin="0,10,0,0">--richTB--</TextBlock>
<RichTextBox x:Name"B" />
<TextBlock Margin="0,10,0,0">--simpleTB--</TextBlock>
<TextBox x:Name"C" />
</StackPanel>
Now, if the focus is in the buttom TextBox (C), and by a Mouse-click I move focus to the top TextBox within menu (A), and then I hit Enter key, the focus returns to the buttom TextBox (C). This behavior of the MenuItem that Enter key "selects" and returns the focus. so far everything is normal.
But, if i focus RichTextBox (B), and then I go through the top TextBox (A), when I tap Return cursor will remain in the box.
I made another little test, and I listening the PreviewKeyDown & KeyDown events for the TextBox within menu (A). So:
<Menu>
<TextBox Width="200" KeyDown="TB_KeyDown" PreviewKeyDown="TB_PreviewKeyDown" />
</Menu>
What I discovered? The last scenario in which the focus was first RichTextBox (B) and then in TextBox within menu (A), there are no listening to the KeyDown for Enter key!

Related

How to get the selected name,value from WPFAutoComplete:TextBoxAutoCompleteProvider event?

I am new to XAML forms. I have some code written years ago by another programmer which I now need to modify. I am trying to intercept the selection that is applied to the textbox when the user presses enter or clicks the mouse on a selection from the dropdown list.
<TextBox x:Name="txtVendor" HorizontalAlignment="Left" Height="23" Margin="24,299,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250"/>
<WPFAutoComplete:TextBoxAutoCompleteProvider x:Name="txtVendorAuto" DisplayMemberPath="Name" SelectedValuePath="VendorNumber" HorizontalAlignment="Left" Height="100" Margin="24,322,0,0" VerticalAlignment="Top" Width="250" TargetControl="{Binding ElementName=txtVendor}" MaxResults="20" MinWidth="250" MinTypedCharacters="2"/>
I thought it would be similar to C#/Vb code in that I would check the selected index change event and get the values, however the only event that looks close is the "SelectionChanged" which fires with any text input and not the actual selected item. I managed to check for the keydown event in the textbox and look for the enter key and that seems to work, but I cannot determine when someone has selected something via the mouse. I have checked the various mouse events (left mouse button down,up, etc.)
Is there a way to simply get the item from the search when it is selected (keyboard or mouse)?

Up/down key bindings not recognized

I have a textbox and a listbox. The listbox shows search suggestions for the textbox.
I want to highlight the first listbox item when user presses down arrow when the textbox is focused.
Similarly, the textbox should be focused back when the user is as the first element on the listbox and presses the up arrow.
I am using the following code for KeyBindings:
<KeyBinding Key="Down" Command="{x:Static local:SearchView.ApplicationShortCutsCommand}" CommandParameter="{x:Static common:SearchViewCommands.MoveToSuggestions}" />
<KeyBinding Key="Up" Command="{x:Static local:SearchView.ApplicationShortCutsCommand}" CommandParameter="{x:Static common:SearchViewCommands.MoveToQuery}" />
Other keys such as Esc and Enter work fine, although this one doesn't work at all (the associated event isn't fired).
Any suggestions?
The events associated with ListBox up and down have priority.
I ended up using shift up/down instead because there are so many event consumers that use the up and down events.

RichTextBox ignores keystrokes (Home,End,PgUp,PgDn) when IsReadOnly is true

I have a WPF RichTextBox control that I want to behave like a normal RichTextBox in every way except that the user cannot edit the text and the caret is not visible. The issue I'm having is that when I change IsReadOnly to False, the RichTextBox stops reacting to keystrokes. I want it to respond normally to keystrokes like Home, End, PgUp, PgDn, Up, Down. The documentation doesn't mention this behavior.
<RichTextBox
IsReadOnly="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Width="65" Height="20">
<FlowDocument>
<Paragraph>
Line 1
Line 2
Line 3
</Paragraph>
</FlowDocument>
</RichTextBox>
I've tried adding an OnKeyDown event handler, but it doesn't fire. I've watched the events in Snoop as I push keys with the RichTextBox in keyboard focus and no fired events are shown in Snoop. I've tried setting IsDocumentEnabled explicitly to True, but that also has no effect. I've tried wrapping the RichTextBox in a ScrollViewer, but this has no effect either.
Any suggestions for how I can maintain this RichTextBox as IsReadOnly=True, but get the expected keystroke behavior of a RichTextBox?
Set IsReadOnlyCaretVisible to True
<RichTextBox IsReadOnly="True"
IsReadOnlyCaretVisible="True"
... />
If you don't want the caret to be visible then set the CaretBrush to Transparent
<RichTextBox IsReadOnly="True"
IsReadOnlyCaretVisible="True"
CaretBrush="Transparent"
... />

WPF Toolkit MaskTextBox Binding Issue

I have a simple control that has a masked text box:
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
...
<extToolkit:MaskedTextBox Mask="000-000-000" Text="{Binding SerialNumber, UpdateSourceTrigger=PropertyChanged}" />
I also have a key binding on the control:
<UserControl.InputBindings>
<KeyBinding Command="{Binding SearchCommand}" Gesture="Enter" />
</UserControl.InputBindings>
The problem is when SearchCommand is executed I need the value they entered in the masked text box as the criteria for the search. With a regular text box this is no problem but apparently the MaskedTextBox control doesn't play well with PropertyChanged UpdateSourceTrigger.
If I click someplace else (so it looses focus) and then press enter it works, but obviously I don't want to have to do that. Are there any good workarounds for this situation?
You must bind your property to the Value property not the Text.
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Documentation

WPF Unleashed error?

The book says
The WPF Button class only adds two
simple concepts on top of what
ButtonBase already provides: being a
cancel button or a default button.
These two mechanisms are handy short-
cuts for dialogs. If Button.IsCancel
is setto true on a Button inside a
dialog (that is, a Window shown via
its ShowDialog method), the Window is
automatically closed with a
DialogResult of false. If
Button.IsDefault is set to true,
pressing Enter causes the Button to be
clicked unless focus is explicitly
taken away from it.
But in this sample window
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
Background="OrangeRed" >
<StackPanel>
<Label FontWeight="Bold" FontSize="20" Foreground="White">
WPF Unleashed (Version 3.0)
</Label>
<Label> 2006 SAMS Publishing</Label>
<Label>Installed Chapters:</Label>
<ListBox>
<ListBoxItem>Chapter 1</ListBoxItem>
<ListBoxItem>Chapter 2</ListBoxItem>
</ListBox>
<TextBox AcceptsReturn="False">HELLO TEXT</TextBox>
<RadioButton>HELLO RADIO BUTTON</RadioButton>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button IsCancel="True" MinWidth="75" Margin="10">Cancel</Button>
<Button x:Name="OKBUTTON" IsDefault="True" MinWidth="75" Margin="10">OK</Button>
</StackPanel>
<StatusBar>You have successfully registered this product.</StatusBar>
</StackPanel>
</Window>
If I press Enter or even click it, the modal Window(By ShowDialog()) does not get closed (leave aside the return value).
Is that an error in the book ?
I believe it is an error. IsDefault and IsCancel simply means that some access-key-magic is applied when the window is created, so that the button is clicked when you hit 'Enter' and 'ESC' respectively.
If you want a Window to close - you need to:
(from MSDN )
When a dialog box is accepted, it should return a dialog box result of true, which is achieved by setting the DialogResult property when the OK button is clicked
...
Note that setting the DialogResult property also causes the window to close automatically, which alleviates the need to explicitly call Close.

Resources