WPF執行個體秀——不用屬性也Binding(XAML篇)

來源:互聯網
上載者:User

 WPF執行個體秀——不用屬性也Binding

 

本文:

實際編程中,因為我們較多地使用到Binding類的Source與Path,所以可能會有一個思維定式,那就是:有可能作為資料來源的類一定要準備好一些屬性,這些屬性將作為Binding的Path。

如果本著這個思想去設計有可能作為資料來源的類,那麼會有兩個問題出現:

1. 這個類的哪些屬性有可能作為資料來源的Path?是都需要激發NotifyPropertyChanged事件,還是用到了再添加?這很有可能讓這個類遲遲不能封閉。

2. 需要用屬性把一些方法封裝起來,用來暴露給Binding,造成冗餘和語義上的不美觀。

其實,WPF類庫裡有一個名為ObjectDataProvider的類就是專門為瞭解決這個矛盾的——有了這個類,你在設計自己的類的時候就不必總想著把它設計成資料來源的事兒了,該怎麼抽象就怎麼抽象、該怎麼封裝就怎麼封裝。

你可能會問:“如果這個類已經封閉了(不再改動)而我又需要拿它當資料來源了,碰巧所需要的資料是它某個方法的傳回值,沒有對應屬性,怎麼辦?”OK,這就是ObjectDataProvider的用武之地了——使用它,可以在你這個類的執行個體外面加上一層“封裝”(或者說是加個殼兒),使它變成一個標準的Binding資料來源。如果沒記錯的話,這應該是著名的“適配器模式”。

 

下面,我們用一段簡單的代碼來學習如何使用ObjectDataProvider。

 

這個例子簡單到不能再簡單——三個TextBox,在前兩個裡輸入合適的數字,在第三個裡會顯示它們的和。按照UI與邏輯分開的原則,計算加法的功能應該由某個類來實現。

 

後台負責計算的類是這樣:

  1.     public class Calculator
  2.     {
  3.         public int Add(int arg1, int arg2)
  4.         {
  5.             return arg1 + arg2;
  6.         }
  7.         public string Add(string arg1, string arg2)
  8.         {
  9.             int x = 0;
  10.             int y = 0;
  11.             if (int.TryParse(arg1, out x) && int.TryParse(arg2, out y))
  12.             {
  13.                 return this.Add(x, y).ToString();
  14.             }
  15.             else
  16.             {
  17.                 return "Input Error!";
  18.             }
  19.         }
  20.     }

大家看到了,設計這個類的時候,涉及到加法運算的邏輯時,任何一個程式員都會很自然地採用一個方法來實現,而不會為了把它做成一個Binding的資料來源專門把這些方法封裝進屬性裡——這樣就破壞了物件導向的抽象。

 

然後,讓我們看看如何使用ObjectDataProvider來封裝這個類。

 

  1. <Window x:Class="WpfApplicationAdd.Window1"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:local="clr-namespace:WpfApplicationAdd"
  5.         xmlns:system="clr-namespace:System;assembly=mscorlib"
  6.     Title="Add" Height="136" Width="230" Background="SteelBlue">
  7.     <Window.Resources>
  8.         <ObjectDataProvider x:Key="odp" ObjectType="{x:Type local:Calculator}" MethodName="Add">
  9.             <ObjectDataProvider.MethodParameters>
  10.                 <system:String>0</system:String>
  11.                 <system:String>0</system:String>
  12.             </ObjectDataProvider.MethodParameters>
  13.         </ObjectDataProvider>
  14.     </Window.Resources>
  15.     <StackPanel>
  16.         <TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
  17.         <TextBox x:Name="textBox2" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}"/>
  18.         <TextBox x:Name="textBox3" Margin="5" Text="{Binding Source={StaticResource odp}, Mode=OneWay}"/>
  19.     </StackPanel>
  20. </Window>

運行起來之後,你就能看到這樣的結果了:

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.