Search This Blog

Friday, December 23, 2011

Silverlight passing event arguments in InvokeCommandAction; invoke a command when ENTER is pressed in a TextBox; Silverlight MVVM calling method from ViewModel with parameters

We need to trigger a command in ViewModel from View and pass arguments in response to an event. There are a couple of ways to accomplish that.

1. Just call a method from ViewModel:

        <i:EventTrigger EventName="KeyDown">
            <ei:CallMethodAction TargetObject="{Binding}" MethodName="QuickSearchKeyDown"/>

2. Call a Command from ViewModel with parameters:
<TextBox Name="textBox">
        <i:EventTrigger EventName="KeyDown">
                    Command="{Binding SomeCommand}"  
                    CommandParameter="{Binding Text, ElementName=textBox}"/>
        <behaviors:UpdateTextBindingOnPropertyChanged />

3. Call command in ViewModel and pass arguments in response to an event

            <Triggers:ExecuteCommandAction Command="SomeCommand" />

TextBoxEnterKeyTrigger source can be found here.

Wednesday, December 21, 2011

C# how to get underlying types from an anonymous type

If you encounter an anonymous type you can get the underlying types using reflection. In my case the source object was of type ‘EntCls81BD186ACCBAB596592C26438762156AD811E’ i.e. unknown to the compiler = dynamically created.


foreach (var possibleRowMatch in sourceDataGrid.ItemsSource)
    var rowProperties = possibleRowMatch.GetType().GetProperties();
    for (int i = 0; i < rowProperties.Length; i++)
        string propertyName = rowProperties[i].Name;
        Type columnType = rowProperties[i].PropertyType;
        var columnValue = possibleRowMatch.GetType().GetProperty(propertyName).GetValue(possibleRowMatch, null);

        //cast to a columnType
        if (columnValue.GetType() == columnType)
            //do something

Tuesday, December 20, 2011

Silverlight update Text property after each letter; Silverlight TextBox get current text; Silverlight force TextBox to rewrite Text property

Our goal is to force TextBox to refresh its Text property. Unfortunately normally Text property is refreshed ONLY when TextBox loses focus. This in unacceptable in my case.

There are 2 solutions to this problem. Creating a custom TextBox control or using PRISM and UpdateTextBindingOnPropertyChanged  behaviour.

The solution from PRISM is the simplest:

        <behaviors:UpdateTextBindingOnPropertyChanged />

Custom control can be downloaded here.

Thursday, December 15, 2011

Silverlight two lines in a button; Silverlight two polylines in a button; Silverlight draw shapes inside a button

To draw shapes inside a button use Polyline. You use Fill property to set filled shapes color and Stroke to set line’s color. StrokeThickness sets line’s thickness. Points are just X,Y coordinates.

        <Polyline Stroke="Red" StrokeThickness="2" Points="0,0 8,8" />
        <Polyline Stroke="Red" StrokeThickness="2" Points="0,8 8,0" />


Wednesday, December 7, 2011

Notepad++ XML Tools Pretty print Errors detected in content

If you’ve installed XML Tools for Notepad++ and after pressing ctrl+shift+alt+B (pretty format XML with line breaks) you get an error:

“Error in content. […..]”

Look for ANY non-UTF-8 characters in your XML. That could be the case.

Sunday, December 4, 2011

how to install coffee-bytes plugin in eclipse; Eclipse folding plugin; manual folding plugin

I had trouble finding a good folding plugin for eclipse Indigo. The installation details are inserted below. In short: download the archive, extract plugins and features folders to the appropriate plugins and features folders in eclipse.


in User Defined Regions tab use e.g.:

Start identifier: region    End identifier: endregion

In code:

//region SomeName
your code
//endregion SomeName


Installation instructions:

  1. Install plugin
    1. Unpack the downloaded file eclipse-folding-plugin.tar.gz
    2. Copy the contents of the:
      1. features folder => eclipse features folder
      2. plugins folder => eclipse plugins folder
  2. Configure plugin in Eclipse:
    1. Select "Windows->Preferences"
    2. Select "Java->Editor->Folding"
    3. Check the "Enable folding" option
    4. Select "Coffee Bytes Java Folding" in the "Select folding to use:" option
    5. Check "User Defined Regions" in the "General Fold Setting:" option

Thursday, December 1, 2011

Silverlight DoubleAnimation rotate 180 degrees; Silverlight animation rotate image from style; Rotate ToggleButton image

This example shows how to make a ToggleButton with an image that rotates according to toggle button’s state.

1. Add a ToggleButton to your view and set its style. (You can download a standard toggle button’s style from microsoft’s sites)

2. Edit the Style:

    <vsm:VisualStateGroup x:Name="CheckStates">
        <vsm:VisualState x:Name="Checked">
                <DoubleAnimation Duration="0:0:0.25" Storyboard.TargetName="myToggleButtonRotateTransform" Storyboard.TargetProperty="Angle" To="0"></DoubleAnimation>
    </vsm:VisualStateGroup x:Name="CheckStates">

<ContentPresenter RenderTransformOrigin="0.5,0.5"
        <RotateTransform x:Name="myToggleButtonRotateTransform" Angle="0" />


Full style source here.

Silverlight xaml style add image

I will cover adding images with the example of ToggleButton. To add an image to a Silverlight style just do the following:

    <Image Source="/Some.Company.Namespace.ModuleName;component/Styles/Images/image.png" Width="10" Height="10" />

Where Some.Company.Namespace.ModuleName is the assembly name (project properties->Assembly name). This is followed by ‘component’ which denotes project folder. Then we have folder path and image file name.

So the path is: