wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
2007-02-28 11:29
796 查看
我们写个前台的小例子来看下吧。。。
在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就写到这里了.
在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就写到这里了.
相关文章推荐
- wpf学习笔记(2)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(1)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(2)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(1)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
- 【深度学习】笔记6:基于CIFAR10网络,训练自己的分类网络的记录
- WPF and Silverlight 学习笔记(十七):WPF资源(Resource)(2)
- 刚接触计算机啊,记录下自己觉得比较好的网络资源,将长期记(内容暂为空,不要点进来)
- WPF and Silverlight 学习笔记(十六):WPF资源(Resource)(1)
- 走上软件之路,以后就在这里记录自己的学习过程了(2007.12.18笔记)
- WPF入门教程系列一——基础 一、 前言 最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料。为了温故而知新把学习过程记录下来,以备后
- 开一个笔记吧,记录下自己的学习之路
- java学习笔记:【从网络获取图像资源】实现
- WPF学习笔记(4):避免让WPF资源字典变得杂乱臃肿
- 安卓个人学习笔记---使用URL访问网络资源
- python学习笔记,以此开始记录自己的python学习过程
- 开此博客记录自己的学习笔记
- <仅是自己做笔记。。。系列-5>给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数,要求下排每个数都是先前上排那十个数在下排出现的次数。