0

在此处输入图像描述我们有 WPF 应用程序,其中我们在一个表单上使用 DataGrid。我们在该 DATAGRID 中使用了多个 DataTemplateColumn。我需要在一列中取一个按钮,假设“浏览”按钮。现在,当我在编辑模式下单击它时,它会打开文件对话框,当我选择文件时,该文件的路径必须存储在该 DATAGRID 列中。那么如何实现这一点,在编辑模式下浏览按钮和正常模式下该文件的路径。

<toolkit:DataGridTemplateColumn Header="Attachment Copy Of Invoice" Width="180" >
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>

                        <TextBlock x:Name="Attach"  Text="{Binding Path=Attachment,UpdateSourceTrigger=PropertyChanged}" />

                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
                <toolkit:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>

                        <Button Name="Click" Click="Click_Click" ></Button>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellEditingTemplate>
            </toolkit:DataGridTemplateColumn>

代码:

 private void Click_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Set filter for file extension and default file extension

        dlg.DefaultExt = ".txt";
        dlg.Filter = "Text documents (.txt)|*.txt";

        // 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;


        }


    }

我需要存储文件名,即相同 TextBlock 的路径。

4

1 回答 1

0
private void Click_Click(object sender, RoutedEventArgs e) {
   var dlg = new Microsoft.Win32.OpenFileDialog();

    // Set filter for file extension and default file extension

    dlg.DefaultExt = ".txt";
    dlg.Filter = "Text documents (.txt)|*.txt";

    // 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;
        var yourType = ((FrameworkElement)sender).DataContext as YourType;
        yourType .Attachment= filename;
    }
 }
于 2013-10-14T05:50:05.953 回答