Application=Code+Markup 讀書筆記 5-7

bjq_ren發表於2008-08-11

5 StackPanelWrapPanel

WPF設計了皮膚panel,將空間和其它元素放在皮膚上,稱之為Layout佈局。

類的邏輯樹:

       FrameworkElement
              Panel(抽象類)
                     Canvas
                     DockPanel
                     Grid
                     StackPanel
                     UniformGrid
                     WrapPanel

1)檢索元素的方法:

FrameworkElementFindName方法,檢索這個根節點下的所有元素:

            Button btn = FindName("ButtonName") as Button;

使用皮膚的Children索引:

            grid.Children[5];    //返回UIElement型別

            grid.Children.IndexOf(btn);        //返回btn元素所在的索引值,不存在則為-1

通過事件處理器,獲取產生事件的物件,有兩種方法:

            Button btn = sender as Button;     //使用第一個引數

            Button btn = e.Source as Button;   //使用第二個引數

以上程式碼對應於:

            btn.Click += ButtonClick;

以及

        void ButtonClick(Object sender, RoutedEventArgs e) { }

事件處理器的另一種情形,就是不使用的btn.Click += ButtonClick方式為每一個Button新增事件,而是代替使用:

            AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonClick));

此時,sender就是Window物件,這就意味著Window會監視其所有的子元素的Click事件;而e.Source仍然是被點選的按鈕物件。

當然,也可以讓所有Button的承載器StackPanel來進行監視:

            stack.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonClick));

此時,sender就是StackPanel物件。

2)使用WrapPanel時,一般搭配ScrollViewer

            ScrollViewer scroll = new ScrollViewer();
            Content 
= scroll;
            WrapPanel wrap 
= new WrapPanel();
            scroll.Content 
= wrap;

3)對於ScrollViewer的使用,這是一個複合控制元件,其滾動條端點的箭頭,是一個RepeatButton,這個按鈕上也有Click事件。所以在ScrollViewerClick方法中,要判斷e.Source的轉型是否成功:

            Button btn = e.Source as Button;

            
if (btn != null)
            

                
//do something
            }

4)在有限的空間內放入很多元素,傳統使用ScrollViewerWPF建議使用ViewBox

            Viewbox view = new Viewbox();
            Content 
= view;
            view.Child 
= grid;

第6 StackWrap

1GridSplitter

GridSplitter派生於Thumb,只能使用於Grid,而且要指定它所在的rowcolumn位置(以及行和列的span)

            GridSplitter split = new GridSplitter();
            split.Width 
= 6;
            grid.Children.Add(split);
            Grid.SetColumn(split, 
2);
            Grid.SetRow(split, 
1);

GridSplitter可以和其它元素共享相同的單元格,這就存在被遮擋的問題——後出現的元素在前面,為此,需要設定margin,以避免重疊:

            split.Margin = new Thickness(10);

一般會讓GridSplitter跨越整行或整列,比較合理:

            Grid.SetRowSpan(split, 3);

            Grid.SetColumnSpan(split, 3);

一般把GridSplitter單獨放在一個或一組單元格中

2UniformGrid

類似於Grid,不同之處在於,UniformGrid所有行都等寬,所有列都等高,可以簡單進行設定:

            UniformGrid unigrid = new UniformGrid();
            unigrid.Rows 
= 2;
            unigrid.Columns 
= 3;

UniformGrid沒有附屬屬性,來為其內部的元素指定位置,只能根據加入元素的順序,依次增加索引值。

               Unigrid.Children.Add(btn);

7

1Polygon的使用:

            Polygon poly = new Polygon();

            poly.Points = new PointCollection();

新建立的Polygon物件,其Points屬性為null。為此之後要新增一系列Point到其中:

            poly.Points.Add(new Point(0, 0));

            poly.Points.Add(new Point(1, 1));

另一種使用方法,直接新增Point陣列:

            poly.Points = new PointCollection(new Point[]
            
{
                
new Point(0,0),
                
new Point(1,1)
            }
);

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15123181/viewspace-422938/,如需轉載,請註明出處,否則將追究法律責任。

相關文章