开发者

Why the need for Binding in this context

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="{Bindi开发者_如何转开发ng 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}}"


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:

  • Data Binding Overview
  • Binding Class


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜