我们来看下一个例子,这个例子将在一个button中放置一个stackpanel,然后在放置多个对象:
1: private void StackInButton()
2: {
3: Button btn = new Button();
4: btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
5: btn.VerticalAlignment = System.Windows.VerticalAlignment.Center;
6: Content = btn;
7:
8: StackPanel sp = new StackPanel();
9: btn.Content = sp;
10:
11: sp.Children.Add(ZigZag(10));
12:
13: Uri uri = new Uri("pack://application:,,/Image/word.jpg");
14: BitmapImage bitmap = new BitmapImage(uri);
15: Image img = new Image();
16: img.Source = bitmap;
17: img.Stretch = Stretch.None;
18: sp.Children.Add(img);
19:
20: Label lab = new Label();
21: lab.Content = "ddddd";
22: sp.Children.Add(lab);
23:
24: sp.Children.Add(ZigZag(0));
25: }
26:
27: private Polyline ZigZag(int offset)
28: {
29: Polyline p = new Polyline();
30: p.Stroke = SystemColors.ControlTextBrush;
31: p.Points = new PointCollection();
32: for (int i = 0; i < 100; i++)
33: {
34: p.Points.Add(new Point(i,(i + offset) % 20));
35: }
36: return p;
37: }
传统的做法是用group box将一组radio button归放到一起,我们来看一个例子:
1: private void RadioButtonTest()
2: {
3: GroupBox gb = new GroupBox();
4: gb.Margin = new Thickness(56);
5: gb.Padding = new Thickness(5);
6: gb.Header = "WindowStyle";
7: Content = gb;
8:
9: StackPanel sp = new StackPanel();
10: gb.Content = sp;
11:
12: sp.Children.Add(CreateRadioButton("WindowStyle None", WindowStyle.None));
13: sp.Children.Add(CreateRadioButton("WindowStyle ThreeDBorderWindow", WindowStyle.ThreeDBorderWindow));
14: sp.Children.Add(CreateRadioButton("WindowStyle SingleBorderWindow", WindowStyle.SingleBorderWindow));
15: sp.Children.Add(CreateRadioButton("WindowStyle ToolWindow", WindowStyle.ToolWindow));
16:
17: AddHandler(RadioButton.CheckedEvent, new RoutedEventHandler(RadioOnClick));
18: }
19:
20: private void RadioOnClick(object sender,RoutedEventArgs e)
21: {
22: RadioButton rb = e.Source as RadioButton;
23: WindowStyle = (WindowStyle)rb.Tag;
24: }
当然你也可以使用RadioButton的GroupName属性来进行RadioButton的分组:
1: rb.GroupName = "firstGroup";
与StackPanel最接近的就是WrapPanel了,最开始的WPF设计时,Wrap比StackPanel还要早的出现,当WrapPanel的内容大于其可显示范围时,WrapPanel会自动换行显示,WrapPanel具有ItemHeight和ItemWidth可以让子项按一定的高度、宽度显示,同时还具有Orientation property,和StackPanel是一样的。
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Diagnostics;
6: using System.IO;
7: using System.Windows.Controls;
8: using System.Windows;
9:
10: namespace WPF_StackAndWrap
11: {
12: class FileSystemInfoButton : Button
13: {
14: FileSystemInfo info = null;
15: public FileSystemInfoButton()
16: :this(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)))
17: {
18: }
19:
20: public FileSystemInfoButton(FileSystemInfo info)
21: {
22: this.info = info;
23: Content = info.Name;
24: if (info is DirectoryInfo)
25: {
26: FontWeight = FontWeights.Bold;
27: }
28: Margin = new Thickness(10);
29: }
30:
31: public FileSystemInfoButton(FileSystemInfo info, string str)
32: :this(info)
33: {
34: Content = str;
35: }
36:
37: protected override void OnClick()
38: {
39: if (info is FileInfo)
40: {
41: Process.Start(info.FullName);
42: }
43: else if (info is DirectoryInfo)
44: {
45: DirectoryInfo dic = info as DirectoryInfo;
46: Application.Current.MainWindow.Title = dic.FullName;
47: Panel pan = Parent as Panel;
48: pan.Children.Clear();
49: if (dic.Parent != null)
50: {
51: pan.Children.Add(new FileSystemInfoButton(dic.Parent,"..."));
52: }
53: foreach (FileSystemInfo item in dic.GetFileSystemInfos())
54: {
55: pan.Children.Add(new FileSystemInfoButton(item));
56: }
57: }
58:
59: base.OnClick();
60: }
61: }
62: }
1: private void WrapPanelTest()
2: {
3: ScrollViewer sv = new ScrollViewer();
4: Content = sv;
5:
6: WrapPanel wp = new WrapPanel();
7: sv.Content = wp;
8:
9: wp.Children.Add(new FileSystemInfoButton());
10: }