Wednesday, October 17, 2012

Windows8: HTML/JS data-binding with template

There are few ways to perform data-binding with template in Windows8 metro app using HTML5/JS. 

I will try to capture some of the data-binding options using template with WinJS.UI.ListView as an example control.

DataExample.itemList.dataSource is the object containing the list of names { name: "Hello" }. 
  • Simple data binding: (Declarative)
If you are having just a single HTML page (default.html), then simple data-binding can be performed using following HTML code snippet
             
< div id="mediumListIconTextTemplate" 
     data-win-control="WinJS.Binding.Template" >
                <div >
                    <!-- Displays the "title" field. -->
                    <input type="text" data-win-bind="innerText: name"/input>
                </div>
            </div>     

  <div id="myFriends" 
       data-win-control="WinJS.UI.ListView"
       data-win-options="{ 
            itemDataSource : DataExample.itemList.dataSource, 
            itemTemplate: mediumListIconTextTemplate
}">
            </div>

This simple binding would work if there are no nested pages. 
  • Binding when there are navigation involved (Declarative)
If there are navigation pages involved then use Select('#<YourTemplateId>'). Rest of the code remains same as in Simple data binding.
  • Template in Javascript (programmatically)
Another way to create a template is in the javascript. You can create template as a javascript object in your .js file. 

var mediumListIconTextTemplate  WinJS.Utilities.markSupportedForProcessing(function mediumListIconTextTemplate(itemPromise) {
    return itemPromise.then(function (currentItem) {
        var result = document.createElement("div");

// Display name
        var displayName = document.createElement("input");
        displayName.type = "text";
        displayName.value = currentItem.data.name;
        result.appendChild(displayName);
        
        return result;
    });
});

Make sure to use "currentItem.data" when accessing the properties. For every item in the collection, the function mediumListIconTextTemplate is called.

You can also add event handlers similar to any standard js code.

 displayName.addEventListener("change", function (event) {
              // Code to handle on change event here
        });

Programmatically creating the template gives high flexibility and handling the elements at a very granular form. But it increases the number of lines of code.

If you want simple binding (readonly without events), I suggest to use simple binding. But if you want more control on your template, then programmatically creating a template would be an option.

Thursday, July 15, 2010

Drag and drop in Silverlight

Attached is the module with drag and drop implementation. Sample implemented is for labels - same can be extended to custom controls or any Silverlight control.

I have the runnable copy attached as well. You can check this out before looking into the code. (draganddrop.xap - under /debug)

The project is created in VS 2010 but I am not using anything specific to SL 4. You can easily migrate it to VS 2008 (SL 3) without issues (of course, you have to copy & paste the code)


Download here

Saturday, May 1, 2010

Setting data context to list box style in silverlight

I had a requirement to bind the data to a list box defined in a style for a usercontrol.
Here is the code which worked for me after some struggle ... I spent about 2 hours in figuring out this fix with some help over the internet.

Backend data context binding code...
public class DeviceObjectData
{
public string Name { get; set; }
}
Binding
Object.DataContext = new List(){
// list of device objects with name...
}


Monday, March 29, 2010

Silverlight ag_e_network_error Code: 4001

I faced this error when running silverlight app. The UI team gave me the XAML files in a solution which was working perfectly fine.

ag_e_network_error.. errorCode: 4001

I knew that silverlight is not able to load the image file rather locate the image file. I did some google search and tried out the said solutions but none of them worked.

Image format could be an issue.. but not in my case as it was working with the solution provided by UI team.

Next thing was to do some trial and error. I hate doing this but this was the only way out as what happens within silverlight is a black box.

1. I tried different path combinations "/" vs "\".
2. Direct path "my dirve:\path...\*.png"
Solution:
3. Then I tried to other way of looking this problem. Since, I was running in debug mode, it struck me that silverlight run time is looking for the images at \Bin\Debug.
Thats it copying the images here worked like a charm.

Also, I copied the images to web folder\ClientBin where my xap will be hosted. It depends whether you want to test when in debug mode from VS or releasing it for users.

I hope this saves some of your time.

Thursday, March 25, 2010

PRISM Framework - Silverlight Data binding problem

PRISM Framework - Silverlight Data binding problem.

I faced the following problem with my Silverlight code...
1. Simple data binding to the silverlight control in the XAML file does not work.
2. Same code when used outside the ZFS app works fine (local project on my machine).

Binding method:
XAML way...

Back end code (Data context)
{
MyTextBlock.DataContext = new TextBindData() { TextValue = "Onewireless-ZFS program" };
}
Public class TextBindData
{
public string TextValue {get; set;}
}

3. The above code doesn’t throw any error but the binding just doesn’t work. This may be due to the PRISM framework usage.

4. If the control is created at run time and binded then it works.
Fix: Create the user control at run time….
...
TextBlock MyTextBlock = new TextBlock();
… // Add the control to Layout root

Now the binding should work just fine.

If you find a better fix for this problem; please let me know.

Wednesday, March 10, 2010

Silverlight Error: The name ‘InitializeComponent’ does not exist in the current context

I faced problem after conversion silverlight 2.0 app to silverlight 3.0 (VS 2008). After searching on net, I found the problem and the following change fixed my problem

Step1 : Right click on project folder -> unload the project
Step2: Right click on the project and click "Edit the Project.csproj"
Step3: Make the following change. Change "SilverlightPage" tag to "ApplicationDefinition" tag name.

Before Change:


After change:

Step4: Save the changes
Step5: Reload the project (Right click & reload)

This must fix the problem.