WPF 点击DataGrid中按钮无触发以及上传文件

上干货

 <DataGridTemplateColumn Header="操作" Width="1*">
                      <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                                    <Button Style="{StaticResource CcHotelButtonStyle}"  Margin="2 0 2 0" Width="60" Height="30"

                Command="{Binding DataContext.OprationQMUploadButtonCommand,UpdateSourceTrigger=PropertyChanged,
                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}
">签名</Button> <Button Style="{StaticResource CcHotelButtonStyle}" Margin="2 0 2 0" Width="60" Height="30"
                Command="{Binding DataContext.OprationLookFileButtonCommand,
                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}
">查看</Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
        /// <summary>
        /// 签名
        /// </summary>
        public RelayCommand<Window> OprationQMUploadButtonCommand { get; set; }
        /// <summary>
        /// 查看
        /// </summary>
        public RelayCommand<Window> OprationLookFileButtonCommand { get; set; }
      public void OprationQMUploadButton(Window win) 
        {
            // MessageBox.Show("OprationQMUploadButton");
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = "*.png";
            ofd.Filter = "图像文件(*.bmp,*.png, *.jpg) | *.bmp;*.png; *.jpg | 所有文件(*.*) | *.*";
            // ofd.InitialDirectory=this.pa
            Nullable<bool> result = ofd.ShowDialog();
            string path = string.Empty;
            if (result == true)
            {
                path = ofd.FileName;
                var citem = DataGridQmZlListData.Where(c => c.idno == id_qmzlStr).FirstOrDefault();
                if (citem != null)
                {
                 
                    string fileName = System.IO.Path.GetFileName(path);

                    using (FileStream stream = File.OpenRead(path))
                    {
                        string basePath = AppDomain.CurrentDomain.BaseDirectory;
                        string myFilePath = $"{Login.HotelCode}\\{DateTime.Now.Year}\\{DateTime.Now.Month}\\Uploads";
                        string fulldicPath = Path.Combine(basePath, myFilePath);//, fileName);
                        string fullFilePath = Path.Combine(basePath, myFilePath, fileName);

                        if (!Directory.Exists(fulldicPath))
                            Directory.CreateDirectory(fulldicPath);
                        using (FileStream wstream = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            byte[] by = new byte[1024 * 1024];
                            while ((stream.Read(by, 0, by.Length) > 0))
                            {
                                wstream.Write(by, 0, by.Length);
                            }
                            citem.sign_map = fullFilePath;
                            MessageBox.Show("上传ok!");
                        }
                    }
                }
            }
        }

 

 

posted @ 2022-11-16 15:52  天天向上518  阅读(211)  评论(0编辑  收藏  举报