I have small app using only barcode scanner and numpad keyboard. For this reason is force focus to textbox (txtInput) and all application control is done via the keydown event for this textbox.
After scan of bacode, it's showed content of box. Now i scan more codes and they add or subtract from the contents of the box. I display the contents of that box in the datagrid.
The problem is when the content of the box is larger than it fits on the screen. I need to scroll the datagrid with keys. Now I have it done like this:
UInt16 list = 1;
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// process of barcode
}
else if (e.Key == Key.Add)
{
if(list < dataGridTickets.Items.Count)
{
dataGridTickets.ScrollIntoView(dataGridTickets.Items[list]);
list++;
}
}
else if (e.Key == Key.Subtract)
{
if(list > 1)
{
dataGridTickets.ScrollIntoView(dataGridTickets.Items[list - 2]);
list--;
}
}
}
It's working, but I have to press the plus key 20 times before I reach the bottom item and it starts scrolling down. The same problem when I want to scroll back.
Is there another way to scroll the datagrid than via an item based scrollintoview?
Is there a way to find out the number of displayed items so that I can add it to the "list" variable? (a different number of items will fit on a different display resolution)
Is there a way to programmatically control the position of the datagrid slider directly?
Or is there any other solution?
If you get a reference to the internal
ScrollViewer
using theVisualTreeHelper
:...you could use its
ScrollToVerticalOffset
method to scroll to to a specified vertical offset position:There is also a
ScrollToBottom
method and aScrollToTop
method if that's what you need. OrScrollToEnd
/ScrollToHome
.