Monday, April 29, 2013

Controls that implement WPF Commanding

I was trying to find a quick list of the basic WPF controls that support commanding (so that I could expose a DelegateCommand from my ViewModel to which I could bind the view). What I found out was that “generally” commands that support commanding implement ICommandSource. Based on that info, I was able to generate the following list of controls that implement ICommandSource:

System.Windows.Input.InputBinding
System.Windows.Input.KeyBinding
System.Windows.Input.MouseBinding
System.Windows.Controls.MenuItem
System.Windows.Controls.Button
System.Windows.Controls.Primitives.ToggleButton
System.Windows.Controls.CheckBox
System.Windows.Controls.GridViewColumnHeader
System.Windows.Controls.Primitives.CalendarButton
System.Windows.Controls.Primitives.CalendarDayButton
System.Windows.Controls.Primitives.DataGridColumnHeader
System.Windows.Controls.Primitives.DataGridRowHeader
System.Windows.Controls.Primitives.RepeatButton
System.Windows.Controls.RadioButton
System.Windows.Documents.Hyperlink
System.Windows.Shell.ThumbButtonInfo

MS.Internal.Documents.DocumentGridContextMenu+EditorMenuItem
System.Windows.Documents.TextEditorContextMenu+EditorMenuItem
System.Windows.Documents.TextEditorContextMenu+ReconversionMenuItem

More info:

MSDN: commanding overview: http://msdn.microsoft.com/en-us/library/ms752308.aspx

Monday, March 18, 2013

Convert an Anonymous type object to a dictionary

I wanted to write a method that would take an anonymous object and have it spit out key,value pairs from the properties defined on that anonymous object.

For example:

new {j="j1",k="k1"}

Should create a dictionary with 2 keys (j,k) which have corresponding values of (j1,k1).

A while back I had written about an helper method that would convert all the properties on an object into a dictionary: http://blog.aggregatedintelligence.com/2013/01/convert-objects-property-values-to.html

It turns out that the same method can be used even for anonymous types!

Monday, March 11, 2013

Did you know–XOR swap algorithm?

Did you know that you can use the XOR operator to swap the values in 2 variables without the use of a 3rd variable?

Check it out:

int x = 111;
int y = 3333;

x ^= y;
y ^= x;
x ^= y;

x.Dump();
y.Dump();

Outputs:

3333
111

Sunday, March 10, 2013

MemoryStream exception–Memory stream is not expandable

Just got hit by this exception.

I was creating a memory stream from bytes that I had read from a file:

var fileBytes = File.ReadAllBytes(filePath);
MemoryStream ms = new MemoryStream(fileBytes, true);

The memory stream was then being used in a bunch of operations that could add data and when it did, it caused the “Memory stream is not expandable” exception to be thrown.

Found out that its because of the constructor that I was using. Because I was providing memory-stream the actual bytes, it was creating the memory stream as a non-expandable one. Instead, if you construct it and then write to it, the memory stream has no such restriction and it can expand.

MemoryStream ms = new MemoryStream();
ms.Write(fileBytes, 0, fileBytes.Length);

Monday, March 04, 2013

Dilbert–Gratuitous displays of mental superiority!

but I try not to frighten ordinary people with any gratuitous displays of mental superiority

UnityBootstrapper–Order in which methods are called

The MSDN documentation page “Initializing Prism Applications” shows the basic stages of the bootstrapping process, but it leaves out when some of the methods that can be overriden get called.

Process as defined in MSDN:

Gg430868.544A7BF3FF93BE95A370A3D97774244A(en-us,PandP.40).png

 

Here is the order in which all methods that can be overridden are called:

  1. Run (called typically when app is starting up)
  2. CreateLogger
  3. CreateModuleCatalog
  4. ConfigureModuleCatalog
  5. CreateContainer
  6. ConfigureContainer
  7. ConfigureServiceLocator
  8. ConfigureRegionAdapterMappings
  9. ConfigureDefaultRegionBehaviors
  10. RegisterFrameworkExceptionTypes
  11. Create Shell
  12. InitializeShell
  13. InitializeModules

And here is logging output of the Prism bootstrapper process (where UnityLoggerFacade is a logging class that I created):

UnityLoggerFacade              - Logger was created successfully.
UnityLoggerFacade              - Creating module catalog.
UnityLoggerFacade              - Configuring module catalog.
UnityLoggerFacade              - Creating Unity container.
UnityLoggerFacade              - Configuring the Unity container.
UnityLoggerFacade              - Adding UnityBootstrapperExtension to container.
UnityLoggerFacade              - Configuring ServiceLocator singleton.
UnityLoggerFacade              - Configuring region adapters.
UnityLoggerFacade              - Configuring default region behaviors.
UnityLoggerFacade              - Registering Framework Exception Types.
UnityLoggerFacade              - Creating the shell.
UnityLoggerFacade              - Setting the RegionManager.
UnityLoggerFacade              - Updating Regions.
UnityLoggerFacade              - Initializing the shell.
UnityLoggerFacade              - Initializing modules.
UnityLoggerFacade              - Bootstrapper sequence completed.

Wednesday, February 27, 2013

Telerik ASP.Net MVC Grid–Ajax binding boolean property

When you bind a collection of objects that have a boolean property on them and if you use server side binding, the columns are automatically rendered as check-boxes. But instead, if you use AJAX binding with the telerik MVC grid, they get rendered as literal texts (“true” or “false”). To fix it, what you need to do is to set the ClientTemplate. Here is an example:

grid.Columns(columns => {
columns.Bound(o => o.Value).ClientTemplate("<input type='checkbox' <#=Value?'checked':''#> disabled />");

Where the code that’s highlighted is the binding code and “Value” is the property to which I am binding. Also, I am using the ternary operator to output “checked:’’” if its true, else it outputs nothing.

  

Sunday, February 24, 2013

Javascript–Convert URLs in text to links

Was looking for some code to convert urls in strings to url anchors. Came across this post (JMRWare) and below is the extracted code for javascript:

Thursday, February 21, 2013

ASP.Net MVC–Authorize filter that loads users and roles from the application configuration file

I needed the ability to set the Authorization filter from a config file (instead of setting the roles and users directly on the controller). The main reason for this is that I needed different roles to be authorized to hit the action based on the environment to which it was deployed (dev,qa, prod).

Here is the code: