WPF入门教程系列十五——WPF中的数据绑定(一)

使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能。WPF的数据绑定跟Winform与ASP.NET中的数据绑定功能类似,但也有所不同,在 WPF中以通过后台代码绑定、前台XAML中进行绑定,或者两者组合的方式进行数据绑定。您可以绑定控件、公共属性、XML 或对象,WPF中的数据绑定跟WinForm与ASP.NET相比,更加快捷、灵活和简单。

一、什么是数据绑定

    WPF 中的数据绑定,必须要有绑定目标和要绑定的数据源。绑定目标可以是继承自 DependencyProperty的任何可访问的属性或控件,例如 TextBox 控件的 Text 属性。数据源可以是其他控件的属性,可以是对象实例、XAML 元素、ADO.NET Dataset、XML数据。微软针对XML绑定与对象绑定,提供了两个辅助类XmlDataProvider 和 ObjectDataProvider。

    WPF的数据绑定跟ASP.NET与WinForm中的数据绑定有什么不同呢? 最大不同就是WPF使用{Binding …}这一语句。 

Binding是用来实现界面控件的属性与后台数据之间的绑定,通过这种形式将前台界面与后台数据联系在一起达到界面与数据耦合的目的。

WPF绑定引擎从 Binding 对象获取有关以下内容的信息:

    源对象和目标对象。

    数据流的方向。你可以通过设置 Binding.Mode 属性来指定该方向。

    值转换器(如果存在)。你可通过将 Converter 属性设置为用来实现 IValueConverter 的类的一个实例,指定值转换器。 

    WPF与ASP.NET与WinForm中的绑定方式比较,存在着如下几点差异:

   (1) Binding may be achieved by coupling the data interface XAML statement. If the ratio of Binding bridge for data, then it is Binding of both ends of the source and destination. Where is the data coming from the source, Binding is a bridge in the middle of the frame, Binding data to target where to go. In general, the source object Binding logical layers, the control object is a target UI Binding layer, so that the data will flow through the UI layer served Binding was UI presentation layer, will complete the process of driving the UI data. As shown below.

 

   (2) Binding property has an important Mode, data flow to achieve the binding. There are several specific.

 

Member name

Explanation

Default

Mode using the default value of binding targets. Each dependency property default values ​​are different. In general, user-editable control properties (properties such as text boxes and check boxes) default to two-way bindings, whereas most other properties default to one-way binding. Determining dependency property binding is unidirectional or bidirectional programming is by default: GetMetadata get attributes using attribute metadata, then check BindsTwoWayByDefault Boolean attribute.

 OneTime

When the application is started or the data context changes, updates binding target. This binding type applies to the following conditions: a snapshot of the current state of the data or for the use of the actual state of static data. If the attribute initialized from the source with a target attribute value, data is not known in advance and the context can also use this binding type. This type of binding is substantially simplified form OneWay binding, in the case where the source does not change the value may provide better performance.

OneWay

When the binding source (source) changes, update the binding target (target) property. This binding type is suitable for bound controls implicit read-only control the situation. For example, the source can be bound to such as stock tickers. Alternatively, it may target property is not used to make changes (such as data binding table background color) control interface. If you do not need to monitor the target property changes, use the OneWay binding mode avoids the overhead of TwoWay binding mode.

OneWayToSource

 When the target property changes update the source property.

 TwoWay

Resulting in changes to the source property or the target property to automatically update each other. This binding type is suitable for editable forms or other fully interactive UI program.

 

    (3) the trigger can be configured, the user determines the data at the input interface to modify the value when the data source. UpdateSourceTrigger properties can be achieved by, there are several specific values

 

Member name

Explanation

Default

UpdateSourceTrigger default binding target property value. Most dependency property defaults are PropertyChanged, while the Text  default property value of LostFocus.

Determining dependency property UpdateSourceTrigger programming default values ​​is used to get the properties GetMetadata metadata attribute, and check the value DefaultUpdateSourceTrigger property.

Explicit

Update binding source only when you call UpdateSource method.

LostFocus

When the binding target element loses focus, update binding source.

PropertyChanged

When the binding target property changes, update binding source immediately.

 

 

 

   Specific usage is as follows:

<TextBox Name="itemNameTextBox"

         Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />

 

 

 

Second, the simple binding

    Next is the first example of this, a very simple binding example that demonstrates how to display the selected values ​​to the ListBox TextBlock by way binding.

    First, add a ListBox to seven ListBoxItem, as ListBox option.

    其次,把第二个 TextBlock 的 Text通过 Binding 与 ListBox 选择项进行绑定。Binding 语法中的 ElementName 属性指示 TextBlock 的 Text 属性要与其绑定的控件的名称。Path 属性指示我们将绑定到Text属性上ListBox元素的属性。具体代码如下。

复制代码
复制代码
<Window x:Class="WpfApp1.WindowBindData"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WindowBindData" Height="400" Width="500">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="150"/>

            <RowDefinition Height="150"/>

            <RowDefinition Height="138*"/>

        </Grid.RowDefinitions>

 

        <StackPanel Grid.Row="0">

            <TextBlock Width="248" Height="24" Text="股票名称:"

        TextWrapping="Wrap"/>

            <ListBox x:Name="listStockName" Width="248" Height="56">

                <ListBoxItem Content="全通教育"/>

                <ListBoxItem Content="大智慧"/>

                <ListBoxItem Content="宝钢股份"/>

                <ListBoxItem Content="浦发银行"/>

                <ListBoxItem Content="工商银行"/>

                <ListBoxItem Content="中国建筑"/>

                <ListBoxItem Content="中国南车"/>

            </ListBox>

            <TextBlock Width="248" Height="24" Text="你所选中的股票名称:" />

            <TextBlock Width="248" Height="24" Text="{Binding ElementName=listStockName, Path=SelectedItem.Content}">

            

            </TextBlock>

        </StackPanel>

 

    </Grid>

   

</Window>
复制代码
复制代码

效果如下图。

 

https://www.cnblogs.com/zzw1986/p/7583534.html

Guess you like

Origin www.cnblogs.com/Jeely/p/11076338.html