wpf学习笔记

1.菜单:普通菜单、上下文菜单(ContextMenu)
<Menu HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" 
Width="517">
<MenuItem Header="文件"></MenuItem>
<MenuItem Header="编辑"></MenuItem>
<MenuItem Header="视图"></MenuItem>
</Menu>
2.DockPanel:让元素靠左右上下; <DockPanel>
<TextBox DockPanel.Dock="Bottom" Text="caonia"></TextBox>
<Button DockPanel.Dock="Left" Content="niameigouride"></Button>
<Button DockPanel.Dock="Right" Content="wocao"></Button>
<Label DockPanel.Dock="Top" Content="nima"></Label>
</DockPanel>

3.Toolbar:放入其中的控件都有新的默认外观;
显示图标的方法:Button的Content中放Image;
<ToolBar DockPanel.Dock="Top" Height="30">
<Button>
<Image Source="1.ico"></Image>
</Button>
<Button Content="新建"></Button>
<CheckBox Content="自动保存"></CheckBox>
<TextBox Text="自动保存"></TextBox>
</ToolBar>
4.多窗口:
窗口名字:window Title="";
居中:window WindowStartupLocation="CenterScreen"
不能缩放:window ResizeMode="NoResize";
窗口默认最大化:WindowState="Maximized";
5.传值:
Main窗口:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
String txt=mainTxt.Text;
AboutWindow aw = new AboutWindow();

aw.TxtMy = txt;
aw.Show();
}
AboutWindow 定义一个TxtMy 属性接收值;
public partial class AboutWindow : Window
{
public String TxtMy { get; set; }
public AboutWindow()
{
InitializeComponent();
}

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
aboutTxt.Text = TxtMy;
}
}
6.ipw.ShowDialog();用ShowDialog打开的窗口,如果设置了DialogResult(bool?) 
为true或flase会关闭窗口;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
InputWindow ipw = new InputWindow();
bool? b=ipw.ShowDialog();
if (b==null)
{
MessageBox.Show("没设置");
}
else if(b==true)
{
MessageBox.Show("确定");
}
else
{
MessageBox.Show("取消");
}
}
7. OpenFileDialog :
private void Button_Click_2(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//文件过滤器
ofd.Filter = "文本|*.txt|png|*.png|jpeg|*.jpg";
if (ofd.ShowDialog()==true)
{
String str = ofd.FileName;
//MessageBox.Show("打开了"+str);
imagemy.Source=new BitmapImage(new Uri(str));
}
else
{
MessageBox.Show("取消了");
}
}
ofd.FileName;//得到文件名


你可能感兴趣的:(WPF)