WPF – Creating bindings in XAML by StaticResource

Shen HengBin's Blog

Here is a memo of how to create bindings in XAML .

As an example, let’s  look at the following snippet.

I have defined three types resource in above code.

  • PageTitle : String
  • FriendItemSource : List of custom class
  • ItemTemplate : DataTemplate of each item.

Then , We will add StaticResource into our XAML

  • Add xmlns reference to the namespace.
  • Add the static resources.


I had to wrap “x:Array ” in an ObjectDataProvider to get it to work, and replace

{StaticResource FriendItemSource}” to “{Binding Source={StaticResource FriendItemSource}}

or not , you will get an error like bellow

“Cannot convert the value in attribute ‘ItemsSource’ to object of type ‘System.Collections.IEnumerable’. ‘System.Windows.Markup.ArrayExtension’ is not a valid value for property ‘ItemsSource’ …”

The final effect looks as following

Full of  XAML source.

View original post


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s