WPF Datagrid 超链接右键单击

发布于 2024-08-12 17:17:43 字数 2937 浏览 4 评论 0原文

我有一个数据网格,其中有一个超链接列,其中包含以下代码:

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

正如您所看到的,我使用单元格上的上下文菜单来获取它们的值,使用以下代码:

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

这显然不适用于超链接列,因为它是模板列。

有没有办法仍然可以复制链接?

我尝试在超链接本身上使用上下文菜单,但它不起作用,对象引用错误。

非常感谢,

问候,

-WtFudgeE-

I have a datagrid which has one hyperlink column with the following code:

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

As u can see i use a contextmenu on the cells to get their value, using the following code:

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

This obviously doesn't work on the hyperlink column since it's a templatecolumn.

Is there a work arround to still get the link copied?

I tried with a context menu on the hyperlink itself but it didn't work, object references were wrong.

Thanks a bunch,

regards,

-WtFudgE-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-08-19 17:17:43

只要用户在打开上下文菜单之前选择应复制的单元格,您的代码就适合我。我使用下面的后台代码来测试它。您看到什么问题?

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) {}
    }
}

Your code works for me, as long as the user selects the cell that should get copied before opening the context menu. I used the below behind code to test it. What issue are you seeing?

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) {}
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文