Reputation: 852
I need to copy the text content of the wpf Text block. I am having a text block within a DataGrid I need to copy the text of the textblock on selecting the datarow .
my xaml code is here ..
<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>
Upvotes: 1
Views: 999
Reputation: 936
If you are using MVVM pattern, You have to create the following command on your ViewModel that copy the content of your 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));
}
}
Then on XAML, use System.Windows.Interactivity to invoke your command when your DataGrid Selection is changed:
<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>
I supposed that your TextBox text binding a property of your model.
Note that you need to reference System.Windows.Interactivity.dll to your project. I hope this helps you!
Upvotes: 1