C# WPF项目实战

2021-11-01 10:40:05 浏览数 (1)

好久没写原创了,今天心血来潮,打算写一篇,关于特定的知识点之前写过很多,今天呢就写一篇综合性的偏应用的一个小的项目实战.

01

重要的知识点

本篇内容基于CM框架编写,涉及以下知识点:

① CM框架下一个控件附加多个事件:

代码语言:javascript复制
cal:Message.Attach="[Event MouseRightButtonDown]=[datagrid_MouseRightButtonDown($source,$eventArgs)];[Event LoadingRow]=[DG_LoadingRow($source,$eventArgs)]"

②datagrid添加行号:

代码语言:javascript复制
DG.LoadingRow  = new EventHandler<DataGridRowEventArgs>(DG_LoadingRow);
代码语言:javascript复制
        public void DG_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex()   1;
        }

③datagrid右键点击添加菜单:

代码语言:javascript复制
 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;
        }

④浏览选择文件路径和浏览选择文件

代码语言:javascript复制
  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);
            }

        }
代码语言:javascript复制
 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:

代码语言:javascript复制
      /// <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:

代码语言:javascript复制
 /// <summary>
        /// bitmap转换成imagesoure
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        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并实现互斥

代码语言:javascript复制
                <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获取.

0 人点赞