【问题标题】:copy text content from textblock within the datagrid wpf从数据网格 wpf 中的文本块复制文本内容
【发布时间】:2014-12-12 06:10:32
【问题描述】:

我需要复制 wpf Text 块的文本内容。 我在 DataGrid 中有一个文本块,我需要在选择数据行时复制文本块的文本。

我的 xaml 代码在这里..

  <DataGrid ItemsSource="{Binding ScenarioTraceLogDetails}"  AutoGenerateColumns="False" CanUserAddRows="False" 
                                                RowHeaderWidth="0" Margin="10,0,10,10" Grid.Row="2" HorizontalAlignment="Stretch" FontSize="14">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Log Description" Width="4*" MinWidth="550" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="TestAutomationClient:ScenarioTraceLogDetailWrapper">
                            <TextBlock Text="{Binding Path=LogDetail.Data}" TextWrapping="Wrap" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Type" Binding="{Binding Path=LogDetail.LogType}" CanUserResize="True" Width="120" MinWidth="120" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Category" Binding="{Binding Path=LogDetail.Category}"  Width="150" MinWidth="150" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Start Time" Binding="{Binding Path=LogDetail.StartTime}"  Width="150" MinWidth="150" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Completion Time" Binding="{Binding Path=LogDetail.CompletionTime}"  MinWidth="150" Width="150" IsReadOnly="True"/>
                    <DataGridTextColumn Header="Duration (~ms)" Binding="{Binding Path=TimeTaken}"  Width="120" MinWidth="120" IsReadOnly="True"/>
                </DataGrid.Columns>
                <DataGrid.Resources>
                    <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=IsError}" Value="true">
                                <Setter Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsWarning}" Value="true">
                                <Setter Property="Foreground" Value="DarkOrange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsSuccessfulInformation}" Value="true">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                        <Setter Property="ToolTip" Value="{Binding Path=ScriptDatasetInfo}" />
                        <Setter Property="ToolTipService.ShowDuration" Value="60000" />
                    </Style>
                </DataGrid.Resources>
            </DataGrid>

【问题讨论】:

  • 在回答您的问题之前,您是否使用 MVVM 模式?
  • 是的,我正在使用 mvvm 模式

标签: wpf datagrid wpf-controls


【解决方案1】:

如果您使用 MVVM 模式,您必须在 ViewModel 上创建以下命令来复制 TextBox 的内容:

public ICommand CopyValueCommand
        {
            get
            {
                return new CommandHandler(

                    () => 
                         // Here your Text box should bind a property of your model
                         // Copy your model property value to ClipBoard
                         Clipboard.SetText(this.SelectedElement.YourProperty));
            }
        }

然后在 XAML 上,使用 System.Windows.Interactivity 在您的 DataGrid 选择发生更改时调用您的命令:

<DataGrid ItemsSource="{Binding Path=Elements, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding Path=SelectedElement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding Path=CopyValueCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
                ...
            </DataGrid.Columns>
        </DataGrid>

备注

  • 我认为您的 TextBox 文本绑定了模型的属性。

  • 请注意,您需要将 System.Windows.Interactivity.dll 引用到您的项目中。 我希望这可以帮助你!

【讨论】:

  • 谢谢..如何将模型的属性绑定到文本块。?
  • @Gowthaman 分享您的 XAML 代码(至少是 DataGrid 的代码)以帮助您。
  • @Gowthaman 我上传了 Visual Studio 项目,下载示例并告诉我你是否正确drive.google.com/file/d/0BwSNnbUsies3YjVhMWROZ0tzVmM/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2011-09-28
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多