C# WPF项目实战
Posted dotNET编程大全
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# WPF项目实战相关的知识,希望对你有一定的参考价值。
好久没写原创了,今天心血来潮,打算写一篇,关于特定的知识点之前写过很多,今天呢就写一篇综合性的偏应用的一个小的项目实战.
01
—
重要的知识点
本篇内容基于CM框架编写,涉及以下知识点:
① CM框架下一个控件附加多个事件:
cal:Message.Attach="[Event MouseRightButtonDown]=[datagrid_MouseRightButtonDown($source,$eventArgs)];[Event LoadingRow]=[DG_LoadingRow($source,$eventArgs)]"
②datagrid添加行号:
DG.LoadingRow += new EventHandler<DataGridRowEventArgs>(DG_LoadingRow);
public void DG_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
③datagrid右键点击添加菜单:
public void datagrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
dGrid = (System.Windows.Controls.DataGrid)sender;
menu1 = new System.Windows.Controls.ContextMenu();
System.Windows.Controls.MenuItem menuitemFunc1 = new System.Windows.Controls.MenuItem();
System.Windows.Controls.MenuItem menuitemFunc2 = new System.Windows.Controls.MenuItem();
System.Windows.Controls.MenuItem menuitemFunc3 = new System.Windows.Controls.MenuItem();
menuitemFunc1.Header = "移动到此位置";
menuitemFunc2.Header = "删除此行信息";
menuitemFunc3.Header = "导出数据";
menuitemFunc1.Click += MoveToPostion_Click;
menuitemFunc2.Click += DeleteRow_Click;
menuitemFunc3.Click += ExportData_Click;
menu1.Items.Add(menuitemFunc1);
menu1.Items.Add(menuitemFunc2);
menu1.Items.Add(menuitemFunc3);
menu1.StaysOpen = true;
}
④浏览选择文件路径和浏览选择文件
public void Load()
{
//lblStr = "you hit mine! i am lable";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图片|*.jpg;*.jpeg;*.bmp;*.png;*.gif";
openFileDialog1.FilterIndex = 1;//当前使用第二个过滤字符串
openFileDialog1.RestoreDirectory = true;//对话框关闭时恢复原目录
openFileDialog1.Multiselect = false;
openFileDialog1.Title = "选择文件";
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Windows.MessageBox.Show("你选择了文件" + openFileDialog1.FileName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public void BrowseSavePath()
{
FolderBrowserDialog browserDialog = new FolderBrowserDialog();
browserDialog.Description = "请选择路径";
try
{
if (browserDialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(browserDialog.SelectedPath))
{
System.Windows.MessageBox.Show("文件夹路径不能为空");
return;
}
BrowseDataSavePath = browserDialog.SelectedPath;
DataExport(BrowseDataSavePath);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
⑤wpf图片显示的两种操作方式;
方式1:
/// <summary>
/// 图片加载显示完成后释放
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
public static BitmapImage LoadImageFreeze(string imagePath)
{
try
{
BitmapImage bitmap = new BitmapImage();
if (File.Exists(imagePath))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
{
bitmap.StreamSource = ms;
bitmap.EndInit();
bitmap.Freeze();
}
}
return bitmap;
}
catch (Exception)
{
return null;
}
}
方式2:
/// <summary>
/// bitmap转换成imagesoure
/// </summary>
/// <param name="hObject"></param>
/// <returns></returns>
[ ]
public static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
{
throw new System.ComponentModel.Win32Exception();
}
return wpfBitmap;
}
⑥datagrid添加RadioButton并实现互斥
<DataGridTemplateColumn Header="Radio" MinWidth="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding GroupSelect,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" GroupName="Mutex" IsEnabled="{Binding RadioEnabled}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
02
—
操作演示
03
—
结尾
链接:https://pan.baidu.com/s/1c8Iv8G-n_oi2iLCpgVl0oQ
提取码联系小编zls20210502获取.
以上是关于C# WPF项目实战的主要内容,如果未能解决你的问题,请参考以下文章