Yet another coding blog covering everything from C#, WPF, SQL, and Silverlight to any other random thought that passes my way…
RSS icon Home icon
  • WPF Datagrid and the dreaded double-tab focus issue

    Posted on November 24th, 2009 biggert 5 comments

    So I just had to figure this out on my own over the last hour or so and found it to be a good candidate for a post. I found my path through the internet on this one but apparently everyone’s solution didn’t exactly equal to mine for some reason or another. So here we go:

    Problem:

    When using a DataGridTemplateColumn from the DataGrid in the WPF Toolkit in .NET 3.5 (after attending PDC09, I know .NET4 will have a built-in WPF DataGrid), if you want to simply use tab to change the focus between controls within the cells, you’ll have to double-tab to move to the next control.

    For example, if I have a textbox in a cell and want to move to a textbox in another cell within the datagrid with TAB, hitting TAB once will move me to the next DataGridCell… then I have to press TAB again to move to the TextBox inside that DataGridCell.

    Solution:

    After googling around, I frequently hit this site which supposedly fixed this problem with a nice little solution: http://blog.yalovoi.net/2009/08/21/

    For some reason unknown to me and not worth my time to investigate, it doesn’t work at all. Here’s what I came up with which is uglier but, in my case, works. I simply navigate the tree from the DataGrid to the textbox.

    Forgive my quick and dirty coding but it gets the job done… I’m sure you could easily spawn a much more generic code to do this:

            private void uxFieldMappingsDataGrid_CurrentCellChanged(object sender, EventArgs e)
            {
                uxFieldMappingsDataGrid.BeginEdit();
            }
    
            private void uxFieldMappingsDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
            {
                ContentPresenter cp = (ContentPresenter)e.EditingElement;
    
                TextBox destinationTextBox = VisualTreeHelper.GetChild(cp, 0) as TextBox;
    
                if (destinationTextBox != null && destinationTextBox.Name == "uxTest")
                {
                    destinationTextBox.Focus();
                }
            }
    

    You can see where I climb the visual tree down to the text box. Very simple code for a very specific reason that I’m sure you can mold to make your own if you find that you are having the same problems as I am (and using code-behind to solve it isn’t against your religion).

  • Microsoft PDC09 keeping it interesting…

    Posted on November 18th, 2009 biggert No comments

    I’m in LA this week attending Microsoft’s PDC09 and it’s an eye opener. All the little things seemed to be taken care of with .NET 4.0, VS2010, Silverlight 4, and so on. Definitely right up my alley with my previous experience with Adobe Flex and my current experience with WPF and .NET 4.0. Even cooler is the continued marriage between the 2 apps where WPF and SL4 are becoming closer and closer in terms of functionality.

    Once I get my hands on the PDC09 laptop that Microsoft is nice to give out for free (badass little Acer with touchscreen, Win7 Ultimate, Office 2010 beta, etc), I’m gonna go ahead and throw it into the fire with VS2010 beta and see if I can get my Delimited text file altering project kickstarted and running.