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!"); } } } } }
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!