WPF: Databinding and the keypress handlers - wpf

This is my textbox:
<TextBox TextAlignment="Right" Text="{Binding PriorityScore, StringFormat=N2}"
Name="PriorityScoreBox" TextChanged="PriorityScoreBox_TextChanged" />
When I click on the Save button everything works fine.
When I press Control-S, it doesn't save the pending changes in the textbox. This is done via a page-level key press handler.
I cannot use UpdateSourceTrigger=PropertyChanged because it interfers with the string formatting. (The user types "4" and the box auto-changes to "4.00".)
Perhaps there is a way to commit the outstanding changes in a key press event?

Private Sub PriorityScoreBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
If e.Key = Input.Key.S And CBool(e.KeyboardDevice.Modifiers And ModifierKeys.Control) Then
Dim bx As BindingExpression = CType(sender, TextBox).GetBindingExpression(TextBox.TextProperty)
bx.UpdateSource()
End If
End Sub

Related

reflect values to combo box from datagrid on Selection Changed event

I have a combobox and textbox using which values are filled into DataGridViewfor display purpose.
The datagriduses SelectionChanged event.
I am Trying to reflect the same data (when the user moves keys up and down or on mouse click) onto the combobox and textbox on SelectionChanged event of datagrid
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString()
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub
Above code works fine for textbox but not for combobox.
In case of combobox, it reflects data of initially selected row only and not the rest.
How do I fix this?
i guess you have to use this code for combobox
combobox1.items.add('your string here')
in your situation would be like this :
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox1.items.add(DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString())
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub

Keydown event within WPF

I currently have a basic click button in WPF which the user can press in order to search I want to add a keydown event. When using standard VB I can
simply implement an e.keycode, but because I'm using WPF it seems to be different. I have included my basic click function below, could someone add to it just so it accepts the enter key as well?
Private Sub BTNSearch_Click(sender As Object, e As EventArgs) Handles BTNSearch.Click
Dim latitude As Double = Double.Parse(TXTLat.Text.Substring(0, TXTLat.Text.IndexOf(","c)))
Dim longitude As Double = Double.Parse(TXTLong.Text.Substring(TXTLong.Text.IndexOf(","c) + 1))
Dim location = New Microsoft.Maps.MapControl.WPF.Location(latitude, longitude)
Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
BingMap.Children.Add(Pin)
Pin.Location = location
BingMap.Center = location
BingMap.ZoomLevel = "18"
End Sub
Buttons are not really designed to handle keyboard input. If you want a key event to trigger the button you should consider using InputBindings to trigger an ICommand that both a KeyBinding and the button are bound to, like this:
<Window>
<Window.InputBindings>
<KeyBinding Key="F5"
Command="{Binding MessageCommand}" />
</Window.InputBindings>
<Button Command="{Binding MessageCommand}">Click Me</Button>
</Window>
The Window would have its DataContext set to a ViewModel with a MessageCommand property of an ICommand that implements the behavior of the button.
A very good tutorial on input binding can be found here.

Get content Focus on WPF DataGrid

so im really not good at this xaml thing and i tried to look everywhere but couldn't find anything usefull for me, hopefully someone will be able to help me here.
So i have a datagrid with TemplateColumns where i have some controls in it such TextBox's and ComboBox's. What im trying to accomplish here is when i tab from one control i would like to focus on the next control in the same row but what is happening now is the column gets focus and only after that when i press tab again the control will be focus, in less words i have to tab twice to jump from one control to another. My datagrid looks like this:
<DataGridTemplateColumn Header="Omschrijving" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox TabIndex="0" Name="txtOms" Text="{Binding txtOmschrijving}" Width="140" Height="24" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Since no one could give a hint or help i kinda digged down on the internetzz and found a solution, hopefully will help someone else in need:
Private Sub dgKasStaatRegels_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles dgKasStaatRegels.Loaded
Try
Dim CellFocusedChangedHandler As New RoutedEventHandler(AddressOf FocusChangedHandler)
[AddHandler](DataGridCell.GotKeyboardFocusEvent, CellFocusedChangedHandler)
Catch ex As Exception
WriteErrorLog("ucKasStaat", "dgKasStaatRegels_Loaded", ex)
End Try
End Sub
Private Sub FocusChangedHandler(sender As Object, args As RoutedEventArgs)
If args.RoutedEvent.RoutingStrategy = RoutingStrategy.Bubble Then
Dim DataGridCellObj As FrameworkElement = TryCast(args.OriginalSource, FrameworkElement)
If Keyboard.IsKeyDown(Key.Tab) Then
If DataGridCellObj IsNot Nothing Then
Dim txtb As TextBox = TryCast(DataGridCellObj, TextBox)
If txtb IsNot Nothing Then txtb.Focus()
Dim cb As ComboBox = TryCast(DataGridCellObj, ComboBox)
If cb IsNot Nothing Then
cb.Focus()
cb.IsDropDownOpen = True
End If
End If
End If
End If
End Sub
Public Shared Function FindParent(Of T As DependencyObject)(dependencyObject As DependencyObject) As T
Dim parent = VisualTreeHelper.GetParent(dependencyObject)
If parent Is Nothing Then
Return Nothing
End If
Dim parentT = TryCast(parent, T)
Return If(parentT, FindParent(Of T)(parent))
End Function
Quick explanation: on the datagrid load add a CellFocusedChangedHandler handler and in that sub just track if the object inside that row is a Textbox(in my case) and set its focus!
It worked for me!

Selected Index Change on WPF combobox?

I'm converting an app of mine from WinForms to WPF. I came across this problem where in WinForms if I CLEAR a combobox, that does not fire combobox.selectedindexchange. It only fires when the user actually changes the index.
That's the functionality I need. However in WPF I can only find combobox.SelectionChanged. This unfortunately fires on both index change and clearing a combobox (any change).
In WPF how can I only trigger an event when the user changes the index? I'm assuming there's a solution I'm missing that's like the WinForms solution. I'm trying not to play games with global variables and having to track what was previously selected....that's a mess.
Mouseleave is also a mess.
I would greatly appreciate any help!
In your case you can implement in SelectionChanged event:
Private Sub OnSelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim combo = TryCast(sender, ComboBox)
If combo.SelectedItem IsNot Nothing Then
'do something
End If
End Sub
You can remove event handler, invoke clear method and add again event handler.
Example code:
<StackPanel>
<ComboBox Name="myCB"
SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem>test1</ComboBoxItem>
<ComboBoxItem>test2</ComboBoxItem>
<ComboBoxItem>test3</ComboBoxItem>
</ComboBox>
<Button Content="Clear" Click="Button_Click" />
</StackPanel>
MainWindow class:
Class MainWindow
Private Sub ComboBox_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Console.WriteLine("Selected index: {0}", CType(sender, ComboBox).SelectedIndex)
End Sub
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
RemoveHandler Me.myCB.SelectionChanged, AddressOf ComboBox_SelectionChanged
Me.myCB.Items.Clear()
AddHandler Me.myCB.SelectionChanged, AddressOf ComboBox_SelectionChanged
End Sub
End Class

silverlight OpenFileDialog opens a second time

I'm using the openFileDialog in sliverlight 2.0 with vb.net in the back end. I have it wired up and it is working as it is suppose to except that the click event seems to fire twice. It fires the first time, i select the files and click ok. it does the processing. But as soon as i click ok, the click event fires a second time and the dialog box comes up again. that is not what i want and i'm not sure what i'm doing wrong that it comes up that second time. Here is the code.. hopefully someone see's what i'm doing wrong.
<Button x:Name="bOpenFileDialog" Content="2. Import CSV"
Height="30" Width="200" Margin="0,96,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Click="bOpenFileDialog_Click" />
<TextBlock Height="19" Margin="246,26,261,0" VerticalAlignment="Top" Text="TextBlock" TextWrapping="Wrap" x:Name="lblMsg"/>
Private Sub bOpenFileDialog_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles bOpenFileDialog.Click
Me.bOpenFileDialog.IsEnabled = False
' Create an instance of the open file dialog box.
Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog
' Set filter options and filter index.
openFileDialog1.Filter = "LOG Files (*.log)|*.log|All Files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.Multiselect = True
' Call the ShowDialog method to show the dialogbox.
Dim UserClickedOK As Boolean = CBool(openFileDialog1.ShowDialog)
' Process input if the user clicked OK.
If (UserClickedOK = True) Then
Dim rows As Integer = openFileDialog1.Files.Count - 1
ReDim aryIISLogs(rows)
For i As Integer = 0 To openFileDialog1.Files.Count - 1
aryIISLogs(i) = openFileDialog1.Files(i).Name
Next
Process1File()
End If
End Sub
Thanks
Shannon
I think this is because you register the event twice: you have it in the button definition:
Click="bOpenFileDialog_Click"
and in the method definition:
Private Sub bOpenFileDialog_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles bOpenFileDialog.Click
Each of these triggers the event, so you have two popups. If you remove either "Click=" or "handles" you'll get the event to fire only once.

Resources