Binding a Dynamically Created WPF Control’s ItemTemplate to a DataTamplate Defined in XAML

I was asked a question about this on Twitter, so thought I’d create a quick post about it.

The example below shows how to use a DataTemplate that is defined in XAML with a ListView control that is dynamically created in the code behind.

1 Create a New WPF Application

Create  WPF application project in Visual Studio.

2 Create The basic XAML

Replace the contents of the MainWindow XAML to the following:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="DaysDataTemplate">
            <Grid Background="Cyan">
                <TextBlock Text="{Binding}"></TextBlock>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid Name="RootContainer" />
</Window>

This defines a DataTemplate called DaysDataTemplate that simply binds the value and has a cyan background so we can see if it is being applied.

We also give the root container (Grid) a name RootContainer so we can reference it later in the code behind.

More...

SHARE:

Lean Publishing and Keeping Software Soft

Today I released the first iteration of my first ever book via Leanpub.com.

To use the word iteration with the word book seems odd, traditionally books are written in their entirety, go through some reviews and editors and eventually get published into the world. Then at some point some errata is released on the publishers website, followed sometime later by a second edition.

Lean publishing turns this on its head and says that the book can be written bit-by-bit with the readers able to contribute by providing feedback about what they like and don’t like so far and what they’d like to see in a final version. This is great as the author(s) can make a book that better suits the people that will be reading it. Readers that buy an in-progress version get free updates as the book progresses.

My book is called Keeping Software Soft, it is based on the premise that software should be easy to change. It’s aimed primarily at apprentice and journeyman software developers and contains my distilled experiences (so far...) after 12+ years of commercial software development. The book provides practical guidance on how to keep software as soft as we can.

It’s currently about 20% complete, and you can buy it today and get free updates as new chapters are released.

Find out more about the Lean Publishing Manifesto here.

SHARE:

Quick-Start Guide To Using xUnit To Test Your Windows 8 WinRT Store app

Until xUnit officially supports* Windows store apps you can get xUnit working with your WinRT app by doing the following:

In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit.net runner for Visual Studio 2012”.

xUnitRunner

In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit Test Library Template”.

xUnitTemplate

You may have to restart Visual Studio…

More...

SHARE:

Using FeatureToggle In Your Windows 8 WinRT App

I just released an update to my open source feature toggling library FeatureToggle.

Version 1.2.0 add support for using feature toggles in your Windows 8 WinRT .Net (C#) app. For this initial release, the NuGet package for Windows8 app installs the source code, rather than a binary reference. This is so it will always build to whatever architecture you are compiling against (Any CPU, x86, x64, ARM).

This release also provides a XAML value converter (FeatureToggleToVisibilityConverter) to make binding the visibility of UI element to a feature toggle easier.

What To Use Feature Toggles For

Typically feature toggles (sometimes called feature flags, flippers, etc.) enable you to keep one branch of source code and deploy code to production with “half done” stuff. This half done stuff is (typically) toggled at the UI level so it is not available to users.

With Windows 8 in-app-purchases (IAPs), you could also use the FeatureToggle library as a unified way of enabling those extra IAP features that a user has paid for.

Getting Started with FeatureToggle in a Windows 8 App

Install FeatureToggle NuGet Package

Create a new Visual C# Windows Store project (Blank App XAML is fine for this demo).

Install the FeatureToggle NuGet package by either:

  • Right0click project, Manage NuGet Packages, search for FeatureToggle, click the Install button; or
  • In the Package Manager Console window, type “Install-Package FeatureToggle” and hit enter.

You will notice two folders added to the project: “FeatureToggleCode” (which you don’t need to change) and “FeatureToggleSample” which contains an example toggle.

Create a Feature Toggle

Create a new class called “AwesomeFeature” that looks like this:

internal class AwesomeFeature : JasonRoberts.FeatureToggle.AlwaysOffFeatureToggle
{
}

More...

SHARE:

The Complete Beginners Guide to using SQLite SQL database in a Windows 8 App

[Writing this on the train home so may be a bit rushed – sorry :) ]

Install the Bits

  1. In Visual Studio 2012, go to Tools menu, Extensions and updates.
  2. Search for “SQLite for Windows Runtime” – install it.
  3. In your Windows Store app, install the NuGet package called “sqlite-net” – these are the LINQy wrappers over SQLite.

Use SQLite

Create an “entity” that you want to store in a table, e.g. Customer.

Create some plain properties, choose one to be your primary key, e.g.

[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }

 

Create a constant to be the filename of the SQLite db on disk:

private const string dbFileName = "data.dat";

More...

SHARE:

An Open Letter to Scott Hanselman

Hi Scott,

My name’s Jason, you don’t know me, we’ve never met.

I was inspired by your honesty and openness when talking about your experience with cancer to say the following:

Thanks.

I don’t think that there has been a single week in recent years where I haven’t either read some of your words or heard your voice on one of your many audio endeavours (This Developers Life is hugely inspiring by the way).

There is a lot of meanness on the internets. The way you handle yourself is an antidote to that. It inspires me to try and always be kind, or at the least not to respond to meanness with meanness.

Maybe in our industry we have a greater number of introverts, we work with logic and code, sometimes we forget that we and others are human beings too…

Putting yourself out there is a daunting thing for most of us: there is a huge fear of being wrong or looking foolish. I almost didn’t write this because of a fear of looking too “soft” or “touchy feely” or sounding like an “ass kisser”, but I decided to go beyond that fear and write anyway.

Thanks for the continued inspiration.

Thanks for the knowledge.

Thanks for promoting kindness.

It is my sincere wish that kind words such as these become more common on the Internet, and that the above words contribute in some small way to help you, your family and friends through this difficult time.

Best wishes,

Jason.

SHARE:

The Enterprise Re-Imagined with Windows 8

If your company is currently in the process of upgrading to Windows 8, do they see it as a simple OS upgrade to keep up to date; or, do they see it as an enabler to re-imagine how the business operates?

Windows 8 offers the potential to streamline business processes, lower costs, and offer additional customer service opportunities

With an internal app store, domain-joined tablet PCs and innovative leadership, Windows 8 can offer the potential to streamline business processes, lower costs, and offer additional customer service opportunities.

Multiple Internal Apps

Refactoring Legacy Applications

Most companies without excellent software development practices and management end up with big, kitchen-sink. costly-to-maintain software systems.

Internal-facing corporate Windows 8 apps offer a solution to this problem.

More...

SHARE:

Getting Ready to Build Your First Windows 8 App

This article outlines an approach to getting started building you first Windows 8 app, but rather than focus on the technology aspects it focus on the pre-coding activities.

The Idea

So you have a few ideas for your first app, but how do you decide which one to build? It depends on what you want to get out of it. If you want to use your first app as a learning experience or to get some app building experience on your CV then you may not be concerned with monetisation.

Monetising you App

If you are attempting app building as a means of income generation then you’ll want to consider what monetisation options Windows 8 gives you:

  1. Paid app
  2. Free app with adverts
  3. In-app purchases*

*You can also use in-app purchases with paid or free apps and with or without ads.

Regardless of app type, you should offer a version of the app without ads for those people who really dislike them.

The Appeal Spectrum

One way of deciding which app idea to build is to think of its potential appeal. If you imagine this appeal as a spectrum from widest appeal (think Facebook, Twitter, etc.) to those with limited appeal (specialist niche apps, etc.).

On top of this spectrum we can add some potential income factors: will millions of people pay hundreds of dollars for the app, or will millions of people pay a dollar for the app?

More...

SHARE:

What I Learned Building My First 4 Windows 8 Apps

This is a bit of a brain-dump, in no particular order, of things I learned building my first 4 Windows 8 (C# XAML) apps:

- Countdown To

- Big Screen Countdown

- BizBuzBingo

- Code Retreat Countdown

Adding Microsoft Ads to your App

  1. Sign up here: PubCenter
  2. Create a Windows 8 Ad Unit (note the different ad sizes)
  3. Install the Advertising SDK
  4. Add a reference to the ad assembly
  5. Ads the AdControl to your XAML, set the application ID and Ad Unit Id properties to those specified in you ad unit in PubCenter (for testing ads before publication see this DZone article)
  6. Ensure your ad unit control has it’s width and height set to that of your ad unit
  7. Ensure that the app capabilities have Internet enabled (otherwise you wont see any ads show up).

Certification Failure when your App has internet capability

If you app is capable of connecting to the Internet, you must have a Privacy Statement (or link to web based privacy policy) in your app – usually in a custom settings fly-out (from the Settings Charm). You must also have a web-based copy of this hosted somewhere on the Internet as you’ll need to provide the URL to it when you submit your app to the store. If your app has no Internet connectivity enabled, you don’t have to do this.

More...

SHARE: