【问题标题】:Browse Button in DataGridTemplateColumn of Datagrid in WPFWPF中Datagrid的DataGridTemplateColumn中的浏览按钮
【发布时间】:2013-01-28 14:25:29
【问题描述】:

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

【问题讨论】:

  • 显示代码隐藏。 “该文件的路径必须存储在该 DATAGRID 列中” - 哪一列?
  • @Andrey Gordeev:我添加了代码请检查一下。
  • 要将文件路径绑定到哪一列?
  • 同一列即 。 & 选择路径后它应该将该路径绑定到该 TextBlock
  • 它似乎绑定到Attachment 属性。是在ViewModel 中还是在代码隐藏中?

标签: wpf wpf-controls wpfdatagrid wpftoolkit


【解决方案1】:
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;
    }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2013-06-30
    • 2011-06-06
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多