【问题标题】:Why can't I copy (as in copy & paste - ctrl C) from my DataGrid为什么我不能从我的 DataGrid 复制(如复制和粘贴 - ctrl C)
【发布时间】:2012-02-12 09:03:40
【问题描述】:

我无法执行从我的 DataGrid 到剪贴板的复制操作。我想让用户从 DataGrid 中选择一些单元格并将它们粘贴到他想要的任何位置。

当我尝试通过用鼠标单击网格中的一个单元格来选择它时,似乎没有任何反应。单元格/行不会更改为突出显示,并且复制 (ctrl C) 不会执行任何操作。

这是我的数据网格:

    <Grid Margin="2,2,2,2" Background="LightGray">
    <!--  Log  -->
    <Label Name="activityLogLabel"
           Height="28"
           Margin="15,5,0,0"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           Content="Activity log" />
    <Grid Width="500"
          Height="482"
          Margin="15,25,0,0"
          HorizontalAlignment="Left"
          VerticalAlignment="Top"
          Background="White">
        <DataGrid x:Name="log"
                  MaxHeight="Infinity"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="True"
                  ClipboardCopyMode="ExcludeHeader"
                  ClipToBounds="True"
                  DataContext="{StaticResource LogViewModel}"
                  HeadersVisibility="None"
                  IsEnabled="true"
                  IsManipulationEnabled="True"
                  IsReadOnly="True"
                  ItemsSource="{Binding Path=LogData}"
                  RowHeight="NaN"
                  SelectionMode="Extended"
                  SelectionUnit="Cell">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Time, Mode=OneWay}" CanUserResize="False" />
                <DataGridTextColumn Binding="{Binding Text, Mode=OneWay}" CanUserResize="False">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="Foreground" Value="{Binding LogLevel, Mode=OneWay, Converter={StaticResource LogLevelConverter}, ConverterParameter=.}" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>

【问题讨论】:

  • 对.Net不太了解,但是如果你要复制不同的单元格,计算机应该如何在内部表示它?
  • @Shaheer 我真的不关心时间字段。如果我将所有数据作为字符串获取,或者只是能够从 Text 列中获取数据,那会很好
  • 那么我建议的最好的想法可能是在该事件处理程序中为 ctrl+c 组合绑定一个事件 检查选择了哪些单元格(如果有)在字符串中组合值(逗号分隔)并复制那个字符串到剪贴板。 (我希望 .Net 有办法做到这一点)。

标签: c# wpf wpf-4.0


【解决方案1】:

该死的。我现在喂得很傻。 我有一个图像(半透明)显示在数据网格的ON TOP(为我们的日志提供水印)。当用户尝试行时,他实际上是在选择图像...

【讨论】:

  • 哈哈哈哈哈,但至少你得到了问题的答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多