我们写个前台的小例子来看下吧。。。在Window元素下增加下面两行代码:用来设置是否修改窗体的大小ResizeMode="CanMinimize"用来设置窗体显示的大小是否是内容大小SizeToContent="WidthAndHeight"在窗体的xaml中加入
< StackPanel > < Button HorizontalAlignment = " Center " Margin = " 24 " > Just a Button </ Button > < Ellipse Width = " 200 " Height = " 100 " Margin = " 24 " Stroke = " Red " StrokeThickness = " 10 " /> < ListBox Width = " 100 " Height = " 100 " Margin = " 24 " > < ListBoxItem > Sunday </ ListBoxItem > < ListBoxItem > Monday </ ListBoxItem > < ListBoxItem > Tuesday </ ListBoxItem > </ ListBox > </ StackPanel >运行可以看到一个按钮、一个圆圈、一个列表框。
我们把代码修改成下面:
< Window x:Class ="FullWPFWCFWWF.Window1" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Title ="FullWPFWCFWWF" SizeToContent ="WidthAndHeight" ResizeMode ="CanMinimize" Height ="400" Width ="260" > < StackPanel > < Button HorizontalAlignment ="Center" Margin ="24" Click ="ButtonOnClick" > Just a Button </ Button > < Ellipse Name ="elips" Width ="200" Height ="100" Margin ="24" Stroke ="Red" StrokeThickness ="10" /> < ListBox Name ="lstbox" Width ="100" Height ="100" Margin ="24" SelectionChanged ="ListBoxOnSelection" > </ ListBox > </ StackPanel > </ Window >这里可以看到我们给按钮增加了个Click事件。来看看后台代码:
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.ServiceModel; namespace FullWPFWCFWWF ... { /**//// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : System.Windows.Window ...{ public Window1() ...{ InitializeComponent(); foreach (System.Reflection.PropertyInfo prop in typeof(Brushes).GetProperties()) ...{ lstbox.Items.Add(prop.Name); } Title = "MyNewWindow"; } void ButtonOnClick(object sender, RoutedEventArgs args) ...{ Button btn = sender as Button; MessageBox.Show(btn.Content); } void ListBoxOnSelection(object sender,SelectionChangedEventArgs e) ...{ ListBox lstbox = sender as ListBox; string strItem = lstbox.SelectedItem as string; System.Reflection.PropertyInfo prop = typeof(Brushes).GetProperty(strItem); elips.Fill = (Brush)prop.GetValue(null,null); } }} 运行看看结果? ok就写到这里了.
