右键单击并复制c#WPF datagrid

时间:2016-07-12 11:55:41

标签: c# wpf data-binding wpfdatagrid

所以我有一个WPF数据网格,每行大约有8个单元格,为了简单起见,我只提供了相关的单元格,但我希望用户能够右键单击此单元格并将内容复制到窗口中没有左键单击并首先选择它的剪贴板。我尝试了很多代码片段,但似乎无法正常工作。每行都是绑定项目。

我一直在尝试的大多数事情都是使用MouseRightButtonDown事件。有些人尝试过X​​Y位置,有些人使用e.OriginalSource作为FrameworkElement,但我似乎无法使用任何东西。不确定它是否因为它的DataGridHyperlinkColumn而不是示例中使用的其他类型?

我是c#n00b!任何帮助将不胜感激。

    <DataGrid x:Name="eventsDataGrid" AutoGenerateColumns="False" IsReadOnly="true" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="10,143,0,0" VerticalAlignment="Top" Height="295"  CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" BorderThickness="1" HorizontalScrollBarVisibility="Disabled"  FontSize="10" Width="1003" MouseRightButtonDown="eventsDataGrid_MouseRightButtonDown">

        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Copy URL" Click="CopyURL">
                </MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>

        <DataGrid.Columns>

            <DataGridHyperlinkColumn Width="230" Header="URL" Binding="{Binding URL}"  CanUserResize="False">
                <DataGridHyperlinkColumn.HeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="ToolTip" Value="URL of website" />
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        <Setter Property="VerticalAlignment" Value="Center"/>
                    </Style>
                </DataGridHyperlinkColumn.HeaderStyle>

                <DataGridHyperlinkColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="HorizontalAlignment" Value="Center"/>
                        <Setter Property="FontSize" Value="12"/>
                    </Style>
                </DataGridHyperlinkColumn.CellStyle>
            </DataGridHyperlinkColumn>

        </DataGrid.Columns>
    </DataGrid>

1 个答案:

答案 0 :(得分:1)

以下示例显示如何将单个上下文菜单用作多个目标元素的资源。请注意,创建自定义命令而不是“借用”可能是个好主意。 ApplicationCommands.Copy以示范为目的,就像我在这里所做的那样。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Copy"
                        Executed="CopyCommand_Executed"
                        CanExecute="CopyCommand_CanExecute"/>
    </Window.CommandBindings>
    <Window.Resources>
        <ContextMenu x:Key="ctMenu" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Copy URL"
                      Command="ApplicationCommands.Copy"
                      CommandTarget="{Binding}"
                      CommandParameter="{Binding Text}"/>
        </ContextMenu>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="123" ContextMenu="{StaticResource ctMenu}"/>
        <TextBlock Text="456" ContextMenu="{StaticResource ctMenu}"/>
    </StackPanel>
</Window>

命令绑定还需要一些代码(与自定义命令实现不同)

private void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
    Clipboard.SetText(e.Parameter as string);
}

private void CopyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.Parameter as string))
    {
        e.CanExecute = true;
        e.Handled = true;
    }
}