在ContextMenu上通过DataGridTemplateColumn内的TextBlock复制命令 [英] Copy Command On ContextMenu over TextBlock inside DataGridTemplateColumn

查看:84
本文介绍了在ContextMenu上通过DataGridTemplateColumn内的TextBlock复制命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要提问,因为以前从未在TextBlock上执行过此操作。我无法复制其中具有TextBlock的DataGridTemplateColumn的内容,并且已为此分配了一个上下文菜单。



复制的内容为空白。 / p>

当我尝试输入MS时,单词是空白单元格。



我的模板列和ContextMenu如下。



我使用TextBox进行了尝试,但是当启用了文本框并且尽管网格列为只读时,它仍然有效。

 < DataGridTemplateColumn Header = Details Width = * IsReadOnly = True> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< TextBlock Text = {绑定详细信息} TextWrapping =包装>
< TextBlock.ContextMenu>
< ContextMenu>
< MenuItem Header = Copy Command = Copy>< / MenuItem>
< / ContextMenu>
< /TextBlock.ContextMenu>
< / TextBlock>
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>


解决方案

为什么复制的内容为空字符串是TextBlock所做的不支持文本框之类的复制,剪切和粘贴命令。因此,如果使用TextBlock,则需要手动支持这些命令,但是可以使用TextBox支持copy命令,该命令可以用作TextBlock。请检查以下内容。

 < TextBox Background = Transparent BorderThickness = 0 Text = {Binding details} IsReadOnly = True TextWrapping =包装> 
< TextBox.ContextMenu>
< ContextMenu>
< MenuItem Header = Copy Command = Copy>< / MenuItem>
< / ContextMenu>
< /TextBox.ContextMenu>
< / TextBox>






[更新]



首先,下面的代码可以正确执行吗?

 < Window x:Class = WpfApplication1.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>
< Grid>
< TextBox Background = Transparent BorderThickness = 0 Text = test IsReadOnly = True TextWrapping = Wrap>
< TextBox.ContextMenu>
< ContextMenu>
< MenuItem Header = Copy Command = Copy>< / MenuItem>
< / ContextMenu>
< /TextBox.ContextMenu>
< / TextBox>
< / Grid>
< / Window>


Thought of asking because never done this before on a TextBlock. I Am not able to copy the content of the DataGridTemplateColumn where it has a TextBlock in that and i have assigned a context menu to that.

The copied content is blank.

When I tried in MS word is Blank Cell.

My Template Column and ContextMenu are as below.

I Tried using TextBox But it work when the textbox is enabled and in-spite of grid column is readonly. It allows editing and when we disable it won't copy the text.

 <DataGridTemplateColumn Header="Details" Width="*" IsReadOnly="True">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
           <Grid>
            <TextBlock Text="{Binding details}" TextWrapping="Wrap">
              <TextBlock.ContextMenu>
                 <ContextMenu>
                    <MenuItem Header="Copy" Command="Copy"></MenuItem>    
                 </ContextMenu>   
              </TextBlock.ContextMenu>
             </TextBlock>
           </Grid>
   </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

解决方案

Why the copied content is empty string is that TextBlock does not support Copy, Cut and Past Commands like TextBox. So If using TextBlock, you'll need to support these commands manually, but you can use TextBox to support the copy command, which could act as TextBlock. Please check the following.

<TextBox Background="Transparent" BorderThickness="0" Text="{Binding details}" IsReadOnly="True" TextWrapping="Wrap">
    <TextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Copy" Command="Copy"></MenuItem>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>


[updated]

First of all, the code below can be executed correctly?

<Window x:Class="WpfApplication1.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">
    <Grid>
        <TextBox Background="Transparent" BorderThickness="0" Text="test" IsReadOnly="True" TextWrapping="Wrap">
            <TextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Copy" Command="Copy"></MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
    </Grid>
</Window>

这篇关于在ContextMenu上通过DataGridTemplateColumn内的TextBlock复制命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆