wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》

我们写个前台的小例子来看下吧。。。
在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就写到这里了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值