WPF Datagrid超链接右键单击

时间:2009-12-02 09:47:38

标签: wpf datagrid hyperlink mouseclick-event

我有一个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 个答案:

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