Pages

Tuesday 18 January 2011

Moving down a cell in a datagrid when you press Enter

In Stock we have some DataGrids for things like Counts, which we want to be able to enter values for successive rows from the keyboard.. ie, 1 , 3 etc..

While Tab moves across a cell, Enter doesn’t move the cell focus (since we’re using a custom control of a textbox in there, the textbox swallows the enter).

To enable the Enter to work, you can just add a KeyDown handler for the TextBox in the template and then add this code behind :
      private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Enter) || (e.Key == Key.Return))
{
TextBox FocusedControl = Keyboard.FocusedElement as TextBox;

if (FocusedControl != null)
{
TraversalRequest Traversal = new TraversalRequest(FocusNavigationDirection.Down);

FocusedControl.MoveFocus(Traversal);
}
}
}
Nice and simple in the end --- it turns out that the TraversalRequest and FocusNavigationDirection classes/enums are very simple and powerful & not limited to DataGrids at all.

Monday 10 January 2011

ResourceStrings in XAML

I needed to replace some hard-coded text in the xaml templates used in our Till Roll control. There was no easy route through to a general ViewModel that could expose some relevant properties to bind to.
In the end I chose to bind directly to the ResourceStrings resource using an x:Static
...
xmlns:local="clr-namespace:Sandstorm.PointOfSale.Modules.Tender.Resources"
...
<TextBlock Padding="20,0,0,0" Text="{Binding Source={x:Static local:ResourceStrings.V_Discount}, Mode=OneTime}" FontStyle="Italic"/>