Allowing return key in some cells of DataGrid but not in others

As I use the EAInformation screens in AppComplete I found that it would be good to be able to accept multi line input in some grid cells.

As the EAInformation UI is completely built with ViewModels and Styles my first approach was to fix this in the Style. However I had, and still have, a hard time to figure out just how to get the style dynamic enough so that it could look at what it displays so that it can decide if it should accept return or not.

So if you have a different solution for this problem – enlighten me please…

Anyway this is what I have; classes with attributes that has the type “Text”:

image and imageetc…

These attributes will be treated as System.String in ECO maintained code:

Code Snippet
  1. [UmlElement(Id="84e4b74b-ee5e-4642-9bd3-9152959e9264", Index=Eco_LoopbackIndices.SLAText)]
  2. [UmlTaggedValue("Eco.PMapper", "StringAsText")]
  3. [UmlTaggedValue("Eco.Length", "-1")]
  4. public string SLAText {
  5.   get {
  6.     return ((string)(this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.SLAText)));
  7.   }
  8.   set {
  9.     this.eco_Content.set_MemberByIndex(Eco_LoopbackIndices.SLAText, ((object)(value)));
  10.   }
  11. }

But as you see above ECO adds a tagged value that defines the suggested Persistence mapper. I was thinking that I can use this tagged value to allow grid columns that show an attribute with this PMapper to accept return and capture multiline input.

Side note: How does ECO decide to treat Text as String, and How does ECO know to use the StringAsText persistence mapper?
In the install directory ECO finds the EcoDataTypes.XML (C:\Program Files (x86)\CapableObjects\ECO\6.0\config\EcoDataTypes.xml). And in this file ECO finds this listing:

Code Snippet
  1. <Type CanonicalName="Text" Type="System.String">
  2.   <CommonName Name="text" CaseSensitive="false" />
  3.   <CommonName Name="memo" CaseSensitive="false" />
  4.   <LanguageName Language="C#" Name="string" />
  5.   <LanguageName Language="Delphi" Name="string" />
  6.   <LanguageName Language="Oxygene" Name="string" />
  7.   <LanguageName Language="VB" Name="String" />
  8.   <TaggedValue Tag="Eco.PMapper" Value="StringAsText" />
  9.   <TaggedValue Tag="Eco.Length" Value="-1" />
  10. </Type>

You are free to add your own data types to this file, and also to use your own persistence mappers if you have defined your own.

So back to the main issue – how can I inject my own dynamic behavior in the ViewModel driven UI? This is what I did:

Code Snippet
  1. private void HookAllGridsToEnableReturnInTextColumns(ViewModel.WPF.ViewModelWPFUserControl UC)
  2. {
  3.     foreach (var x in UC.ViewModel.AllViewModelClasses.Values)
  4.     {
  5.         foreach (var y in x.Columns)
  6.         {
  7.             if (y.SpawnedArtifacts.ContainsKey("grid"))
  8.             {
  9.                 (y.SpawnedArtifacts["grid"] as DataGrid).PreviewKeyDown +=
  10.                           new KeyEventHandler(EAInformation_PreviewKeyDown);
  11.             }
  12.         }
  13.     }
  14. }

The code above takes a ViewModelWPFUserControl – finds the ViewModel-runtime that by the way is the same for all supported UI-styles (ASP.NET, Winforms, WPF and Silverlight). It looks for ViewModel columns displayed as Grid components – and when it finds one it hooks the PreviewKeyDown event to a local event handler.

Then the event handler is implemented this way:

Code Snippet
  1. void EAInformation_PreviewKeyDown(object sender, KeyEventArgs e)
  2. {
  3.     if (e.Key == Key.Return)
  4.     {
  5.         DataGridCellInfo dgci = (sender as DataGrid).CurrentCell;
  6.         ViewModelClass vmc = ((sender as DataGrid).Tag as ViewModelClass);
  7.         foreach (var x in vmc.Columns)
  8.         {
  9.             if (x.SpawnedArtifacts.ContainsKey("control") && x.SpawnedArtifacts["control"] == dgci.Column)
  10.             {
  11.                 if (x.ModelInfo != null)
  12.                 {
  13.                     ITaggedValue tv = x.ModelInfo.TaggedValues.GetItemByTag(EcoTaggedValues.PMapperName.Tag);
  14.                     if (tv != null && tv.Value == "StringAsText")
  15.                     {
  16.                         IInputElement c=Keyboard.FocusedElement;
  17.                         if (c is TextBox)
  18.                             (c as TextBox).AcceptsReturn = true;
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.     }
  24. }

The code above check if Return is pressed, if so, and if CurrentCell belongs to a column that was implemented by a ViewModelColumn that get its data from a model-class-attribute that use a persistence mapper of StringAsText – if all this is true and the focused element is a TextBox (this is the case when the cell is in edit mode) then we instruct the TextBox to accept return-key-strokes.

image

And that is it. With this article I wanted to show that the ViewModels are by no means a cul-de-sac. They are just a declarative way to get you further in the right direction with less code. You can and should use the meta information from your model to create generic code that does stuff that you want. Not only does this minimize the code you need to maintain – it also raise the quality since code written this way acts the same all over.

This entry was posted in Declarative, ViewModel, WPF. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*