WPF入門教程系列四

WPF之Binding的使用(二)

1、  前言express

初學WPF常常被Binding搞得苦不堪言,Binding的重用性就不作介紹了,在WPF應用程序開發中Binding是一個很是重要的部分。WPF也是近期才接觸,學習WPF也是在網上查資料與微軟的MSDN進行學習,寫本博客的目爲了溫故而知新把學習過程記錄下來,以備後查。app

2、  WPFBindingide

接下仍是用作幾個示例來作演示若是經過綁定把ListBox中選中的值顯示到TextBlock中學習

1.)首先,給ListBox添加ListBoxItem,作爲ListBox的選項 。spa

2.)其次,把TextBlock 的 Text經過 Binding 與 ListBox 選擇項進行綁定。Binding 語法中的 ElementName 屬性指示 TextBlock 的 Text 屬性要與其綁定的控件的名稱。Path 屬性指示咱們將綁定到Text屬性上ListBox元素的屬性。以下代碼所示code

 1 <Window x:Class="Student.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:Student"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="650" Width="800">
 9     <Grid>
10 
11         <Grid.RowDefinitions>
12             <RowDefinition Height="200"/>
13             <RowDefinition Height="200"/>
14             <RowDefinition Height="*"/>
15         </Grid.RowDefinitions>
16 
17         <StackPanel Grid.Row="0">
18             <TextBlock Width="250" Height="26" Text="湖南旅遊景點:" TextWrapping="Wrap" Background="Azure" FontSize="20"/>
19             <ListBox x:Name="listStockName" Width="248" Height="90" Background="Azure" FontSize="20">
20                 <ListBoxItem Content="武陵源"/>
21                 <ListBoxItem Content="南嶽衡山 "/>
22                 <ListBoxItem Content="岳陽樓"/>
23                 <ListBoxItem Content="嶽麓書院 "/>
24                 <ListBoxItem Content="君山島 "/>
25                 <ListBoxItem Content="張家界"/>
26                 <ListBoxItem Content="花明樓 "/>
27                 <ListBoxItem Content="崀山風景名勝區"/>
28                 <ListBoxItem Content="鳳凰古城"/>
29             </ListBox>
30             <TextBlock Width="250" Height="24" Text="你所選中的景點:" Background="Aqua" FontSize="20" />
31             <TextBlock Width="250" Height="30" Text="{Binding ElementName=listStockName, Path=SelectedItem.Content}" Background="Azure" FontSize="26"/>
32         </StackPanel>
33 
34     </Grid>
35 
36 </Window>
View Code

效果圖以下:orm

3、  WPF之綁定模式xml

首先,咱們來作一個簡單示例,這個示例是根據ListBox中的選中項,去改變TextBlock的背景色。將 TextBlock 的背景色綁定到在 ListBox 中選擇的顏色。在下面的代碼中針對TextBlock的 Background 屬性使用綁定語法綁定從 ListBox 中選擇的值。代碼以下。對象

 1 <StackPanel Grid.Row="1">
 2 
 3             <TextBlock Width="300" Height="30" Text="顏色:" FontFamily="幼圓" TextWrapping="Wrap"/>
 4 
 5             <ListBox x:Name="listColor" Width="300" Height="68" FontFamily="幼圓">
 6                 <ListBoxItem Content="Blue"/>
 7                 <ListBoxItem Content="Red"/>
 8                 <ListBoxItem Content="Green"/>
 9                 <ListBoxItem Content="Gray"/>
10                 <ListBoxItem Content="Cyan"/>
11                 <ListBoxItem Content="GreenYellow"/>
12                 <ListBoxItem Content="Orange"/>
13             </ListBox>
14 
15             <TextBlock Width="300" Height="30" FontFamily="幼圓" Margin="3,1,2,1"  Text="改變背景色:" />
16             <TextBlock Width="300" Height="30" Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}">
17             </TextBlock>
18 
19         </StackPanel>
View Code

效果圖以下所示:blog

接下來咱們對上面的示例進行一些修改:

1)同一個數據源綁定到兩個或多個控件上。如咱們的示例中把ListBox的選中項綁定到TextBoxTextBlock

2)在綁定語法中增長一個 Mode 屬性,即綁定模式。對於咱們的示例,咱們把TextBlock的綁定語法中的Mode屬性設爲 OneWay 。把TextBox的綁定語法中的Mode屬性設爲TwoWay

 

對於示例中的Mode進行一下簡單說明(具體能夠參見前一篇):

1)使用 OneWay 綁定時,每當數據源(ListBox)發生變化時,數據就會從數據源流向目標(TextBlock)

2OneTime 綁定也會將數據從源發送到目標;可是,僅當啓動了應用程序或 DataContext 發生更改時纔會如此操做,所以,它不會偵聽源中的更改通知。

3OneWayToSource 綁定會將數據從目標發送到源。

4TwoWay 綁定會將源數據發送到目標,但若是目標屬性的值發生變化,則會將它們發回給源。

 

下面就是修改後的示例代碼,功能是將 TextBlock (OneWay) TextBox (TwoWay) 綁定到 ListBox 的代碼:

 1 <StackPanel Grid.Row="1">
 2 
 3             <TextBlock Width="300" Height="30" Text="顏色:" FontFamily="幼圓" TextWrapping="Wrap"/>
 4 
 5             <ListBox x:Name="listColor" Width="300" Height="68" FontFamily="幼圓">
 6                 <ListBoxItem Content="Blue"/>
 7                 <ListBoxItem Content="Red"/>
 8                 <ListBoxItem Content="Green"/>
 9                 <ListBoxItem Content="Gray"/>
10                 <ListBoxItem Content="Cyan"/>
11                 <ListBoxItem Content="GreenYellow"/>
12                 <ListBoxItem Content="Orange"/>
13             </ListBox>
14 
15             <TextBlock Width="300" Height="30" FontFamily="幼圓" Margin="3,1,2,1"  Text="改變背景色:" />
16             <TextBlock Width="300" Height="30" Text="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}"  Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}">
17             </TextBlock>
18             <TextBox Name="txtTwoWay" Text="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}"  Background="{Binding ElementName=listColor,  Path=SelectedItem.Content,Mode=TwoWay}">
19                 
20             </TextBox>
21         </StackPanel>
View Code

綁定模式如何應用呢?下面是我的的一點看法:

1)當只想讓用戶看到數據,而不但願用戶去修改數據時,能夠採用 OneWay 模式,相似winform中的只讀屬性。

2)當但願用戶能夠對控件中的數據進行修改,同時讓用戶修改的數據更新到數據源(DataSet、對象、XML 或其餘綁定控件)中時,可使用 TwoWay 綁定。

3)若是想讓用戶修改數據源中的數據,而又不想使用TowWay模式,就可使用 OneWayToSource 綁定。OneWayToSource模式容許經過在原來被看做是綁定源的對象中放置綁定表達式,從而翻轉源和目標。

 

4)當你的界面中的一系列只讀控件均被綁定了數據,而且當用戶刷新了數據源時,但願綁定控件中的值仍保持不變,可使用 OneTime 綁定。此外,當源沒有實現 INotifyPropertyChanged 時,OneTime 綁定模式也是一個不錯的選擇。

Bingding的源

有三個屬性用來設置源:ElementName(string)、Source(Object) 和 RelativeSource(RelativeSource)。注:這三個只能指定一個,不然異常。

ElementName: 源爲一個元素(Element),這裏用的是此元素中設置的Name屬性。

Source:以object做爲源。<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>

RelativeSource: 源相對於綁定目標的位置。

                        源是元素自己:{Binding RelativeSource={RelativeSource Self}}

                        源是Tempalte中元素的Parent:{Binding RelativeSource={RelativeSource TemplatedParent}}

                        源是綁定以collection形式的前一個數據:{Binding RelativeSource={RelativeSource PreviousData}},MSDN上關於PreviousData的說明並很少,這裏有一篇文章能夠參考

                        以上三項爲RelativeSource中的Static值,使用這些值能夠減小內存開銷

                        源是Ancestor(可能比parent還高):{Binding RelativeSource={RelativeSource FindAncestor,AncestorLevel=n, AncestorType={x:Type desiredType}}}

Path:

Binding中的Path是 PropertyPath對象。
在最簡單的狀況下,Path 屬性值是要用於綁定的源對象的屬性名稱,如 Path=PropertyName。
經過相似於 C# 中使用的語法,能夠指定屬性的子屬性。例如,子句 Path=ShoppingCart.Order 將綁定設置爲對象的子屬性 Order 或屬性 ShoppingCart。
若要綁定到附加屬性,請將附加屬性用括號括起。例如,若要綁定到附加屬性 DockPanel.Dock,則語法爲 Path=(DockPanel.Dock)。
在應用了索引器的屬性名稱以後的方括號內,能夠指定屬性的索引器。例如,子句 Path=ShoppingCart[0] 將綁定設置爲與屬性的內部索引處理文本字符串「0」的方式對應的索引。此外,還支持多個索引器。
在 Path 子句中能夠同時使用索引器和子屬性,例如,Path=ShoppingCart.ShippingInfo[MailingAddress,Street]。
在索引器內部,能夠有多個由逗號 (,) 分隔的索引器參數。可使用圓括號指定每一個參數的類型。例如,可使用 Path="[(sys:Int32)42,(sys:Int32)24]",其中 sys 映射到 System 命名空間。

若是源爲集合視圖,則能夠用斜槓 (/) 指定當前項。例如,子句 Path=/ 設置到視圖中當前項的綁定。若是源爲集合,則此語法指定默認集合視圖的當前項。
能夠結合使用屬性名和斜槓來遍歷做爲集合的屬性。例如,Path=/Offices/ManagerName 指定源集合的當前項,該源集合包含一樣是集合的 Offices 屬性。其當前項是包含 ManagerName 屬性的對象。
也可使用句點 (.)路徑綁定到當前源。例如,Text=」{Binding}」 等效於 Text=」{Binding Path=.}」。

 

   小結

WPF的核心理念是變傳統的UI驅動數據變成數據驅動UI,支撐這個理念的基礎就是本章講的Data Binding和與之相關的數據校驗和數據轉換。在使用Binding的時候,最重要的就是設置它的源和路徑。

 Data Binding到此講解完畢。本人接觸WPF不到一年,寫的很差勿噴,有不足之處還望你們多多指正。

若有不瞭解能夠參考官方文檔說明

相關文章
相關標籤/搜索