wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
来源:互联网 发布:工作日志记录软件 编辑:程序博客网 时间:2024/04/30 09:36
我们写个前台的小例子来看下吧。。。
在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>
<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>
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);
}
}
}
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学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(1)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(2)《都是自己根据网络资源学习记录的仅供参考》
- wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》
- java学习笔记——synchronized的理解(仅供参考)
- C++虚函数学习笔记(仅供参考)
- wpf的学习记录1
- wpf的binding学习记录
- 记录自己的学习
- 记录自己的学习
- wpf-学习笔记3
- Directshow学习笔记五-----一个简单的视频播放程序(个人学习总结,仅供参考)
- 机器视觉网络资源学习笔记
- WPF的Binding学习笔记(一)
- Openstack juno 与 Docker集成(学习笔记,仅供参考)
- <struts学习>2014.2.8学习笔记(仅以此日志来记录自己的技术路)
- WPF学习记录1
- php学习路线(仅供参考)
- 收集的一些学习SharePoint 2007的网站
- Server Application Error 错误以及解决办法
- 第一个c#软件
- 控制只运行一份程序的另一个方法
- WINDOWS应用程序设计的基本术语
- wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
- 在WinXP上通过Virtual PC安装WinCE
- Ubuntu - Edgy Eft 下安装配置Java开发环境(JDK 6 + Eclipse WTP + Tomcat + MySQL)
- Linux c 文件管理简介
- 利用ASP实现事务处理的方法
- VC文件扩展名说明
- Powerdesigner使用建议(完整版)
- 挂马的ASP码
- Linux的引导过程剖析