2016-07-12 75 views
1

因此,我有一個WPF數據網格,每行有大約8個單元格,爲簡單起見,我只包含相關的單元格,但我希望用戶能夠正確單擊該單元格並將內容複製到Windows剪貼板中,不用左鍵單擊並首先選擇它。我嘗試了很多代碼片段,但似乎無法獲得任何工作。每一行都是綁定的項目。在c#上的右鍵單擊和複製內容菜單WPF數據網格

我一直在嘗試的大部分事情都是使用MouseRightButtonDown事件。有些人已經嘗試了到XY的位置,有些人已經使用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

你可以上下文菜單分配到細胞,而不是'DataGrid'和使用'命令'用'CommandParameter'而不是'Click'事件。通過傳遞單元格內容作爲命令參數,所有內容都應該可用於在後面的代碼中填充剪貼板。 – grek40

+0

謝謝你,這聽起來像我需要的東西,但我不知道如何將我的上下文菜單分配給單元格,XAML的哪一部分現在進入?我在DataGridHyperlinkColumn和DataGridHyperlinkColumn.CellStyle裏面試過它,看起來並不像它,我也找不到任何Web示例。任何想法? – jamie

+1

http://stackoverflow.com/questions/5200687/create-contextmenus-for-datagrid-rows只需進行一些修改即可將項目作爲命令參數傳遞 – michauzo

回答

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; 
    } 
} 
+0

感謝您的回覆,但此代碼似乎表現在和我原來的代碼一樣。當我右鍵單擊一行時,出現複製菜單,但呈灰色顯示,當我左鍵點擊一行(如此選擇),然後右鍵單擊複製菜單,內容可以複製到剪貼板。是預期的嗎?道歉,如果我沒有準確解釋我想要的結果:-) – jamie

+0

我將ContextMenu =「{StaticResource ctMenu}」添加到我的。那是對的嗎? – jamie

+0

不,您應該通過樣式或模板將其添加到單元格/單元格內容中。有關更多詳情,請參閱@michauzo的評論。 – grek40