tags:

views:

48

answers:

2

i am starting WPF, looking at How do o: Getting started with Entity Framework

i am abit confused why the need for

<ListBox Name="ListBox1" ItemsSource="{Binding Source={StaticResource CustomerSource}}" >

why cant i do

<ListBox Name="ListBox1" ItemsSource="{StaticResource CustomerSource}" >

how do i know when i need Binding. because on 1st thought, just like i use a static resource in Styles

<Button Style="{StaticResource someStyle}"

why not

<Button Style="{Binding Source={StaticResource someStyle}}"
A: 

You could also set the DataContext on the parent control instead. If the ListBox is contained in for example a StackPanel for example.

<StackPanel x:Name="parentControl" DatContext="{StaticResource CustomerSource}">
<ListBox x:Name="ListBox1" ItemSource="{Binding}">
...
</ListBox>
</StackPanel>
Sun
+2  A: 

This example assigns a value retrieved from the resources using the specified key to the Text property:

<TextBox Text="{StaticResource SomeText}" />

This examples binds the Text property to a property on an object retrieved from the resources using the specified key:

<TextBox Text="{Binding Source={StaticResource SomeObject}, Path=SomeProperty}" />

The Binding class is used for data binding that is a way to surface data retrieved from a data source on the GUI, allowing users to interact with it. Without data binding values are simply assigned to the controls on the UI.

Bindings add a layer of abstraction between the UI controls and the underlying data source associated with it, providing a bounce of services. Here are some of the most important ones:

  • Automatic propagation of changes in the data between the UI and the data source in either or both directions
  • Conversion/formatting of values
  • Notification through events

Related resources:

Enrico Campidoglio