WPF 体验---对话框调用
很长时间没开博了。继续。。。。
WPF相对于Winform, dialog control是少了几个阿。不过都是一家子,可以必要时拿过来用用了。
做个简单程序调用个dialog。 效果图:
简单的调用,代码:
代码
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static string PicturePath = "";
private void btnShow_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".jpg";
dlg.Filter = "JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.*";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
PicturePath = filename;
}
this.image1.Source = GetImage();
this.image1.Width = this.picPanel.Width;
image1.Height = picPanel.Height;
}
private static BitmapImage GetImage()
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(PicturePath);
image.EndInit();
return image;
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
this.image1.Source = null;
}
private void btnSetBG_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.ColorDialog color = new System.Windows.Forms.ColorDialog();
if (color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.picPanel.Background = new SolidColorBrush(Color.FromArgb(color.Color.A,color.Color.R,color.Color.G,color.Color.B));
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
别忘了引用system.windows.form噢!!
哦对了,这里有个Bug:拉伸窗体的时候只有最后一个Button拉伸变化,其它的不变。什么问题呢? 待研究...