2009-12-02 154 views
0

我有一個與下面的代碼一個超鏈接列中的數據網格:WPF Datagrid的超鏈接右鍵點擊

<dg:DataGrid Style="{StaticResource DataGridStyle}" x:Name="movieGrid" 
     ItemsSource="{Binding ElementName=This, Path=movieData}"> 
     <dg:DataGrid.ContextMenu> 
      <ContextMenu Name="cm"> 
       <MenuItem Header="Copy" Click="CopyCell_Click"/> 
      </ContextMenu> 
     </dg:DataGrid.ContextMenu> 
     <dg:DataGrid.Columns> 
      <dg:DataGridTemplateColumn x:Name="editColumn" Width="40" Header="Edit" CanUserResize="False"> 
       <dg:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button Content="Edit" Height="20" Tag="{Binding Path}" Click="Edit_Click"/> 
        </DataTemplate> 
       </dg:DataGridTemplateColumn.CellTemplate> 
      </dg:DataGridTemplateColumn> 
      <dg:DataGridTextColumn x:Name="titleColumn" IsReadOnly="True" Binding="{Binding Title}" Header="Title" SortDirection="ascending" /> 
      <dg:DataGridTextColumn Visibility="Collapsed" x:Name="titleDiffColumn" IsReadOnly="True" Binding="{Binding IMDBTitle}" Header="IMDBTitle" /> 
      <dg:DataGridTextColumn x:Name="scoreColumn" IsReadOnly="True" Width="60" Binding="{Binding Score}" Header="Score" /> 
      <dg:DataGridTextColumn x:Name="yearColumn" IsReadOnly="True" Width="60" Binding="{Binding Year}" Header="Year" /> 
      <dg:DataGridTextColumn x:Name="genreColumn" IsReadOnly="True" Binding="{Binding Genre}" Header="Genre" /> 
      <dg:DataGridTemplateColumn x:Name="linkColumn" Width="195" Header="Link"> 
       <dg:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Margin="3,0,0,0"><Hyperlink Tag="{Binding Link}" Click="Link_Click"><TextBlock Text="{Binding Link}"/></Hyperlink></TextBlock> 
        </DataTemplate> 
       </dg:DataGridTemplateColumn.CellTemplate> 
      </dg:DataGridTemplateColumn> 
      <dg:DataGridCheckBoxColumn x:Name="seenColumn" Width="60" Binding="{Binding Seen}" Header="Seen"/> 
     </dg:DataGrid.Columns> 
    </dg:DataGrid> 

就像你看到的,我使用的細胞將ContextMenu得到他們的價值,使用下面的代碼:

//copy datagrid item 
    private void CopyCell_Click(object sender, RoutedEventArgs e) 
    { 
     DataRowView itemsSource = movieGrid.CurrentItem as DataRowView; 
     int index = movieGrid.CurrentColumn.DisplayIndex; 
     string cellValue = itemsSource.Row.ItemArray[index - 1].ToString(); 
     Clipboard.SetData(DataFormats.Text, cellValue); 
    } 

這顯然不適用於超鏈接列,因爲它是一個模板列。

是否還有工作要繼續複製鏈接?

我試圖用超鏈接本身的上下文菜單,但它沒有工作,對象引用是錯誤的。

多謝,

問候,

-WtFudgE-

回答

1

您的代碼工作對我來說,只要用戶選擇應該打開上下文菜單之前被複制的細胞。我用下面的代碼來測試它。你看到什麼問題?

using System.Data; 
using System.Windows; 

namespace GridLinkTest 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      DataContext = this; 

      DataTable dataTable = new DataTable("MovieData"); 
      dataTable.Columns.Add(new DataColumn("Title")); 
      dataTable.Columns.Add(new DataColumn("IMDBTitle")); 
      dataTable.Columns.Add(new DataColumn("Score")); 
      dataTable.Columns.Add(new DataColumn("Year")); 
      dataTable.Columns.Add(new DataColumn("Genre")); 
      dataTable.Columns.Add(new DataColumn("Link")); 
      dataTable.Columns.Add(new DataColumn("Seen")); 

      DataRow row = dataTable.NewRow(); 
      row["Title"] = "Watchmen"; 
      row["IMDBTitle"] = "Watchmen"; 
      row["Score"] = 7.8; 
      row["Year"] = 2009; 
      row["Genre"] = "Action"; 
      row["Seen"] = true; 
      row["Link"] = "www.imdb.com/title/tt0409459"; 
      dataTable.Rows.Add(row); 

      movieData = new DataView(dataTable); 
     } 

     public DataView movieData { get; set;} 

     private void CopyCell_Click(object sender, RoutedEventArgs e) 
     { 
      DataRowView itemsSource = movieGrid.CurrentItem as DataRowView; 
      int index = movieGrid.CurrentColumn.DisplayIndex; 
      string cellValue = itemsSource.Row.ItemArray[index - 1].ToString(); 
      Clipboard.SetData(DataFormats.Text, cellValue); 
     } 

     private void Edit_Click(object sender, RoutedEventArgs e) {} 

     private void Link_Click(object sender, RoutedEventArgs e) {} 
    } 
} 
+0

是的,你做的工作,因爲它只是一個文本塊。但如果我添加一個超鏈接的模板列,那麼它不起作用。 – WtFudgE 2009-12-03 10:40:24

+0

什麼是文本塊?我用你提供的XAML。 – 2009-12-03 13:39:36

+1

使用「Wallstreet Programmer」的代碼示例,對我來說也適用。我右鍵單擊超鏈接單元格,出現上下文菜單,選擇「複製」,然後將其粘貼到某處。 – SuperOli 2010-06-23 14:45:04