Skip to main content

Posts

Showing posts from June, 2008

Automatically Triggering Code Generation From A DSL Model

If you've used Microsoft DSL Tools, you know that normally the user has to click 'Transform Templates' button in the Solution Explorer - to start running text templates on your DSL Model. Well, what if you need to trigger the code generation automatically, when ever the model changes? Here is a simple solution.

Firs of all, create a code generator class in your DSL Project - inheriting the TemplatedCodeGenerator. Override the GenerateCodeMethod. The first parameter is the DSL model filename against which you need to generate the code, and the second parameter is the template file content you may execute against your model. You can substitute the templateFileContent with your own template code. Here, I'm loading the same from my resource file.
[Guid("ECF4D6E6-0656-48d6-BD32-8F1EADD98D7F")] public class MyModelCodeGenerator : TemplatedCodeGenerator { protected override byte[] GenerateCode(string modelFileName, string templateFile…

WPF Combo Box - 'Cancelling' A Selection Change

What if you want to 'cancel' a selection change in a WPF combo box?

Yes, you have an event for handling a selection change, like ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) But there is no direct way of reverting the SelectedItem to the previous one, if you don't like the change!! Arggggg!!!

Here is one way of tackling this. The parameter e has two collection properties - AddedItems and RemovedItems. The AddedItems contains the items added to the selection, and RemovedItems contains items removed from the selection. As the Combo box supports only one item selected at a given time, always both these collection properties won't have more than one item in in. So, if you want to revert the selection to the previous item, you can do something like ComboBox combo = (ComboBox)sender; combo.SelectedItem = e.RemovedItems[0]; But if you do it in SelectionChanged event of the Combo Box, setting the SelectedItem will again trigger the Select…