2008年10月31日 星期五

繫結多個不同的集合

WPF 隨手筆記

在WPF 裡若有多個來源 (不同的集合或Table...) 想列在一個清單裡, 除了寫程式之外, 有更簡單的方法嗎?

答案是使用CompositeCollection 類別.

  1. 先定義資料來源

    Window.Resources定義資料來源SuperStarts是一個集合類別, HistoryHeroesData 是一個XML內容

    <Window.Resources>       
    <c:SuperStars x:Key="SuperStartsData"/>
    <XmlDataProvider x:Key="HistoryHeroesData" XPath="HistoryHeroes/Hero">
    <x:XData>
    <HistoryHeroes xmlns="">
    <Hero Name="關公" />
    <Hero Name="花木蘭" />
    <Hero Name="成吉思汗" />
    <Hero Name="岳飛" />
    <Hero Name="文天祥" />
    <Hero Name="鄭成功" />
    </HistoryHeroes>
    </x:XData>
    </XmlDataProvider>

    <DataTemplate DataType="{x:Type c:SuperStar}">
    <TextBlock Text="{Binding Path=Name}" Foreground="Blue"/>
    </DataTemplate>

    <DataTemplate DataType="Hero">
    <TextBlock Text="{Binding XPath=@Name}" Foreground="Green"/>
    </DataTemplate>
    </Window.Resources>


  2. 資料繫結, 使用CompositeCollection

    CompositeCollection裡使用CollectionContainer定義資料來源


    <ListBox Name="myListBox" Height="300" Width="200" Background="White">       
    <ListBox.ItemsSource>
    <CompositeCollection>
    <CollectionContainer Collection="{Binding Source={StaticResource SuperStartsData}}" />
    <CollectionContainer Collection="{Binding Source={StaticResource HistoryHeroesData}}" />
    <ListBoxItem Foreground="Red">Other Listbox Item 1</ListBoxItem>
    <ListBoxItem Foreground="Red">Other Listbox Item 2</ListBoxItem>
    </CompositeCollection>
    </ListBox.ItemsSource></ListBox>



執行結果:


執行結果



SuperStar是一個類別檔, 以下是它的原始程式:


using System.Collections.ObjectModel;
class SuperStar
{
public string Name{ get; set;}
}
class SuperStars : ObservableCollection<SuperStar>
{
public SuperStars():base() {
Add(new SuperStar() { Name = "劉德華" });
Add(new SuperStar() { Name = "張曼玉" });
Add(new SuperStar() { Name = "劉嘉玲" });
Add(new SuperStar() { Name = "梁朝偉" });
}
}


參考來源: http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx

2008年10月26日 星期日

如何在VS 2008對預存程序偵錯

如果要在VS2008 直接對預存程序偵錯的話,

直接使用伺服器總管用有權限的帳號連到資料庫, 然後開啟該預存程序設定中斷點按右鍵選Step Into即可開始偵錯.

這個很簡單.

可是如果要對ADO.NET 的程式碼一路進行偵錯到預存程序, 那麼就必須要有幾個條件了!!

  1. 伺服器總管連到資料庫, 開啟預存程序, 在預存程序設定中斷點.
  2. 在程式碼執行預存程序的位置設定中斷點
  3. 在專案屬性設定, 啟用SQL Server偵錯

    1

  4. 伺服器總管那個連線上, 設定應用程式偵錯

    2

2008年10月18日 星期六

如何控制內容物件的位置及大小

WPF 隨手筆記

Canvas

是一個很像傳統Windows Form設計的容器, 控制項放進去時使用Top, Left決定物件的位置, Width, Height決定物件的大小.

 

Grid

很像HTML的Table, 可以將控制項放在格子內, 如果將Grid以單格使用, 效果很像Canvas.

不過, Grid是內的控制項可以使用Width, Height, Margin, HorizontalAlignment, VerticalAlignment屬性來決定控制項的位置及大小.

  1. 當Width及Height沒有設定或為Auto時(同時HorizontalAlignment, VerticalAlignment屬性不是Stretch時)控制項的大小會依據控制項的內容決定, Ex.Button的Content屬性的大小.
  2. HorizontalAlignment, VerticalAlignment屬性, 如果控制項的Width, Height沒有設定或者為Auto, 這兩個對齊屬性設定成Stretch也會影響控制項的大小, 同時Margin的Right及Bottom對於大小也會有作用, 例如Button設成Stretch以及Margin的Right及Bottom都設為10, 那麼Button會擴展到與右下邊界距離10的大小.

第一種情況: Save按鈕的大小依據內容大小決定.

第1種情況

第二種情況: 111按鈕, 根據水平垂直對齊方向為Stretch擴展大小, 同時與格子的右邊界維持10, 下邊界維持30.

第二種情況

在使用Expression Blend時用到Grid做為LayoutRoot, 在視窗的左上方會有一個按鈕, 可以切換Layout mode, 有兩種模式:

  • Grid is in Canvas layout mode
  • Grid is in Grid layout mode

in Canvas layout mode

上面兩張圖都是在這種模式下, 選取物件時, 只會出現物件的邊界選取圓點, 可以利用這些選取圓點拖拉物件的大小.

in Grid layout mode

按視窗的左上方可以切換兩種模式

切換layout 模式

在Grid layout mode 時Grid的Column及Row的上左方會有如下三種圖案, 代表Column或Row計算大小的方式

Star Sized Pixel sized Auto sized

在選取控制項除了四邊的控點之外也會有與邊界距離的實線或虛線, 實線代表HorizontalAlignment, VerticalAlignment屬性的對齊關係, 上頭的數字則是Margin屬性的數字. 你可以發現實線上頭有數字, 虛線上頭沒有數字代表Margin的那個相對邊值為零.

7