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:

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


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

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

<TextBox>
    <i:Interaction.Triggers>
        <Triggers:TextBoxEnterKeyTrigger>
            <Triggers:ExecuteCommandAction Command="SomeCommand" />
        </Triggers:TextBoxEnterKeyTrigger>
    </i:Interaction.Triggers>
</TextBox>

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:

<TextBox>
    <i:Interaction.Behaviors>
        <behaviors:UpdateTextBindingOnPropertyChanged />
    </i:Interaction.Behaviors>
</TextBox>

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.

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

image

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.

USAGE:

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:VisualStateManager.VisualStateGroups>
    <vsm:VisualStateGroup x:Name="CheckStates">
        <vsm:VisualState x:Name="Checked">
            <Storyboard>
                <DoubleAnimation Duration="0:0:0.25" Storyboard.TargetName="myToggleButtonRotateTransform" Storyboard.TargetProperty="Angle" To="0"></DoubleAnimation>
            </Storyboard>
        </vsm:VisualState>
    </vsm:VisualStateGroup x:Name="CheckStates">
</vsm:VisualStateManager.VisualStateGroups>

<ContentPresenter RenderTransformOrigin="0.5,0.5"
        x:Name="contentPresenter"
        [.... SOME OTHER STANDARD PROPERTIES ...] >
    <ContentPresenter.RenderTransform>
        <RotateTransform x:Name="myToggleButtonRotateTransform" Angle="0" />
    </ContentPresenter.RenderTransform>
</ContentPresenter>

 

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:

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

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:

AssemblyName;component/PathToTheImage