February 21, 2012

What is Binding?

Taken from the following article
What is "binding" and What makes it late? by Eric Lippert
Binding is the association of a syntactic element that names a method with a logical element of the program. So, speaking generally I would say that "binding" is any association of some fragment of syntax with some logical program element.

Basically by "early binding" we mean "the binding analysis is performed by the compiler and baked in to the generated program"; By "late binding" we mean "some aspect of the binding will be performed by the runtime" and therefore a binding failure will manifest as a runtime failure.

Early and late binding might better be called "static binding" and "dynamic binding"; static binding is binding performed using "static" facts known to the compiler, and dynamic binding is performed using facts "dynamically" known to the runtime.

February 17, 2012

WPF/Silverlight Data Binding

WPF Basic Data Binding FAQ - Better explanation than the one below.

WPF Data binding cheat sheet

Data Binding Overview - Basically, the target is the control and the source is the code property. Typically, each binding has these four components: a binding target object, a target property, a binding source, and a path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

http://msdn.microsoft.com/en-gb/magazine/cc163299.aspx - Data Binding in WPF. Good introduction. Good short description of Xml data binding (though not 2 way I notice)

http://msdn.microsoft.com/en-us/magazine/cc700358.aspx - Customize Data Display with Data Binding and WPF. Some good stuff on hierarchical data templates and how to use them to fill a tree view using object data binding.

http://www.codeproject.com/Articles/26270/Understanding-WPF-via-ASP-NET - Nice article comparing WPF and ASP.NET data binding etc.

Also look at these related blogs
DisplayMemberpath
Data Templates

Bind "IsEnabled" of one control "cbXXX" to "IsChecked" property of another with name "cbYYY" in Xaml:
... Name="cbXXX" IsEnabled="{Binding ElementName=cbYYY, Path=IsChecked}" ...

February 16, 2012

DisplayMemberPath

DisplayMemberPath - Allows for simple binding on list type controls. Name or path on bound object that will be displayed for each item in the control.

See this article http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath - Nice ComboBox example that allows the user to chose some colors.

ObjectDataProvider

ObjectDataProvider - Wraps and creates an object that you can use as a binding source. Allows binding to objects that do not have a default constructor and that can be called Asynchronously.
http://www.thomasclaudiushuber.com/blog/2008/01/10/bind-to-methods-with-objectdataprovider/ - Nice simple example of using ObjectDataProvider

Data Templates

Taken from Top Ten UI Development Breakthroughs In Windows Presentation Foundation

You don't have to set the data template on a particular control. If you prefer, you can associate a data template with a particular type, indicating that WPF should use the specified template whenever it encounters that particular type as content.
eg
<Window.Resources> 
<DataTemplate DataType="{x:Type local:Person}"> <StackPanel Orientation="Horizontal"> <TextBlock FontWeight="Bold">Name:</TextBlock>
<TextBlock Text="{Binding Name}" Margin="2,0,10,0" /> <TextBlock FontWeight="Bold">
Date of birth:</TextBlock> <TextBlock Text="{Binding DateOfBirth}"Margin="2,0,10,0" /> </StackPanel> </DataTemplate> 
</Window.Resources>
Anything in the window that uses a Person object as its content will pick up this data template by default.

Example C++/CLI sealed properties and methods

This is to help remember the syntax of this stuff, it is too easy to forget
virtual void EnableStuff() sealed = IStuffEnabler::EnableStuff 
{
  stuffEnableImpl();  
}

virtual property bool IsStuffEnabled 
{
  bool get() sealed = IStuffEnabler::IsStuffEnabled::get
  {
 return stuffIsEnabledImpl();
  }
} 

Or split into source and header
Header file:
virtual void EnableStuff() sealed = IStuffEnabler::EnableStuff;
    
virtual property bool IsStuffEnabled 
{
  bool get() sealed = IStuffEnabler::IsStuffEnabled::get;
}      
and source file:
void StuffEnabler::EnableStuff() 
{
  stuffEnableImpl();  
}
    
bool StuffEnabler::IsStuffEnabled::get()  
{
    return stuffIsEnabledImpl();
}     

January 26, 2012

Lazy<T>

Added in version 4.0 of .Net, a lazy template. The concept of 'Lazy' programming is quite simple, do not create an object until you actually use it. Lazy has 2 properties, "IsValueCreated" and "Value". "IsValueCreated" specifies if the templated object has been created yet or not whilst "Value" when invoked actually creates an instance of the templated item.
Here is a simple example:

public class Thingey
{
    public string Name { get; set; }
}
To test it:
internal void TestLazyTemplate()
{
    Lazy<Thingey> myThingey = new Lazy<Thingey>();
    bool isCreated = myThingey.IsValueCreated;
    Debug.Assert(isCreated == false); // Not created yet
    myThingey.Value.Name = "Sutum";   // Created when accessed
    isCreated = myThingey.IsValueCreated; 
    Debug.Assert(isCreated == true);
}
This is a simple example but does not really show the power of Lazy. Fortuneately, NInject works with Lazy<> parameters. This is a sneaky way to implement late binding. I used this in a recent project to late bind some IPlugin interfaces. Some plugins were injected into a constructor as Lazy plugins. This means that when the constructor was invoked the plugins were not created immediately. It is only when the "plugins.Value" parameter is accessed that NInject actually constructed the plugins. In this case this did not happen in the constructor at all, it is invoked later in another separate "Initialise" method.