Search This Blog

Thursday, February 9, 2012

Silverlight set Binding to property from code-behind or ViewModel

There is a static class BindingOperations which has a method called SetBinding().

BindingOperations.SetBinding(targetObject, TargetObjectClass.AttachableProperty, new Binding());

Where:

targetObject – is the instance of the object we bind to
TargetObjectClass.AttachableProperty – is the static reference to the propety we want to bind from
new Binding() – is the Binding with the Path and Source properties appropriately set.

Sample usage is:

BindingOperations.SetBinding(observableResources,ObservableResources.CurrentCultureProperty, new Binding() { Path = new PropertyPath("CurrentCulture"), Source = LanguageManager, Mode = BindingMode.TwoWay});

You can also use:

targetObject.SetBinding(TargetObjectClass.AttachableProperty, new Binding());

Friday, February 3, 2012

Silverlight sdk:DataGrid Column Header DataBinding; How to set DataBinding in Column Header in Silverlight

There is a problem with setting DataBinding as a Header in Column (e.g. DataGridTextColumn) from Silverlight Client SDK. Unfortunately Header is not a dependency property, do binding to it will result in converting DataBinding to string (calling its ToString() method).

“System.Windows.Data.Binding”

To overcome the problem create a helper with a Dependency property (e.g. HeaderBinding). Change to HeaderBinding binding should set the real Header string.

Usage:

<sdk:DataGridTextColumn Controls:DataGridColumnHelper.HeaderBinding="{Binding Something” />

 

Helper:

public static class DataGridColumnHelper
{
    public static readonly DependencyProperty HeaderBindingProperty = DependencyProperty.RegisterAttached(
        "HeaderBinding",
        typeof(object),
        typeof(DataGridColumnHelper),
        new PropertyMetadata(null, DataGridColumnHelper.HeaderBinding_PropertyChanged));

    public static object GetHeaderBinding(DependencyObject source)
    {
        return (object)source.GetValue(DataGridColumnHelper.HeaderBindingProperty);
    }

    public static void SetHeaderBinding(DependencyObject target, object value)
    {
        target.SetValue(DataGridColumnHelper.HeaderBindingProperty, value);
    }

    private static void HeaderBinding_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DataGridColumn column = d as DataGridColumn;
        if (column == null) { return; }
        column.Header = e.NewValue;
    }
}

Monday, January 23, 2012

.NET C# Jasper Server SOAP Web Services using Microsoft.Web.Services2.dll

I had some problems connecting to Jasper server using SOAP web services and the standard library System.Web.Services.dll.

The solution uses basic connectivity, the runReport() method and Microsoft.Web.Services2.dll from “Web Services Enhancements 2.0 for Microsoft .NET Framework”. You can download the library here.

You need path to reference it Visual Studio:

C:\Program Files (x86)\Microsoft WSE\v2.0\Microsoft.Web.Services2.dll

Whole needed source is available here.

var jasperService = new JasperService("http://localhost:8088/jasperserver/services/repository");
var credentials = new NetworkCredential("perspectiv", "perspectiv");
jasperService.Credentials = credentials;

string requestXML = "[.....]";
                   
jasperService.runReport(requestXML);
var attachments = jasperService.ResponseSoapContext.Attachments;
if (attachments.Count > 0)
{
    var atach = attachments[0];
    var atachStream = atach.Stream;
    using (var fileStream = File.Create("C:\\test\\test.pdf"))
    {
        atachStream.CopyTo(fileStream);
    }
}

Microsoft.Web.Services2 401 Unauthorized

If you encountered this error using Web.Services2 from Microsoft just try:

var service = new JasperService("http://localhost:8088/jasperserver/services/repository");
var cred = new NetworkCredential("someUser", "somePass");
service.Credentials = cred;

If this does not work for you try:

SoapContext requestContext = service.RequestSoapContext;
UsernameToken userToken =
    new UsernameToken("someUser", "somePass",
                      PasswordOption.SendPlainText);
requestContext.Security.Tokens.Add(userToken);
requestContext.Security.Timestamp.TtlInSeconds = 86400;

Wednesday, January 11, 2012

Android delete national characters from SMS by default; Android send sms without polish characters

If you want to send sms without national characters (in other words delete polish or other weird characters from your sms) and save some money on sms bills try this on Android:

If you have Samsung Galaxy S2 (maybe some other Samsung devices as well):
- go to: SMS messages –> Settings –> Input mode –> GSM alphabet
(choose the option GSM alphabet instead of Unicode one and your smses will be sent without national characters)

If you have other devices:
- install “GO SMS Pro” from Market.

Gosms - setting – advanced settings – settings for sending - switch on national setting

This application has the option called:

“Localization support for accented chars: by enabling Czech, Polish and French SMS mode “ which will delete your national characters from smses in Android.

Tuesday, January 10, 2012

C# Using reflection to create an instance of a specific type; convert string to a specific type;

The idea behind the post is to convert a string such as: “System.Int32” to an instance of a specific type.
To create an instance of a compiled custom class in the assembly:

Assembly.GetExecutingAssembly().CreateInstance(YourCustomClassName);

To create an instance of a built-in type e.g. “System.Int32” :
Activator.CreateInstance(Type.GetType("System.Int32"));

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.