Search This Blog

Wednesday, February 29, 2012

Silverlight animating PopupMenu–‘IsOpen’ property, translate transform and opacity

This sample shows how to animate System.Windows.Controls.Primitives.Popup ‘IsOpen’ property, its position and opacity.

To change the visual state from code behind use:

VisualStateManager.GoToState(this, "HiddenState", true);

VisualStateManager.GoToState(this, "VisibleState", true);

source code.

Thursday, February 9, 2012

Using StringFormat in Binding

I had a problem using string format for a simple task of just "following the string from Binding with a ‘:’ sign. This can accomplished with:

Content="{Binding a,Source={StaticResource b}, StringFormat='{}{0}:'}"

Don’t forget the ‘’ signs.

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;
    }
}