在WPF中的Datagrid的DataGridTemplateColumn中浏览Button

时间:2013-02-13 05:15:12

标签: wpf wpf-controls wpfdatagrid wpftoolkit

enter image description here我们有WPF应用程序,我们在一个表单上使用DataGrid。 我们在DATAGRID中使用了多个DataTemplateColumn。 我需要在一列中选择一个按钮,假设是“浏览”按钮。 现在,当我在EDIT模式下单击它打开文件对话框时,当我选择文件时,该文件的路径必须存储在该DATAGRID列中。 那么如何实现这一点,在编辑模式下BRowse按钮&在普通模式下该文件的路径。

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

CODE:

 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的路径。

1 个答案:

答案 0 :(得分: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;
    }
 }