简体   繁体   中英

How to make TextBlock Text Selectable on a WPF DataGrid Group Header or Anyother way to do it

I am working on displaying rows which are grouped on WPF DataGrid

Group Header which I am currently displaying is a TextBlock, and the text displayed on the TextBlock is not selectable.

How can I make TextBlock text selectable so that I can copy the value.

Following is the XAML.

I am using following code to Bind data to the grid and group the data.

Entity:

public class AverageCounter
{
    public string CounterName { get; set; }
    public string Role { get; set; }
    public string RoleInstance { get; set; }
    public decimal CounterAverageValue { get; set; }
}

Code to Bind and Group.

var results = new ListCollectionView(queryResultSet);

if (results.GroupDescriptions != null)
{
    results.GroupDescriptions.Add(new PropertyGroupDescription("CounterName"));
}

dataGrid1.AutoGenerateColumns = true;
dataGrid1.ItemsSource = results;

XAML:

<Window x:Class="CheckPerfromanceCounters.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:CheckPerfromanceCounters"
        Title="MainWindow" Height="390" Width="878">
    <Window.Resources>
        <local:AvgConverter x:Key="avgConverter"/>
    </Window.Resources>
    <Grid>

        <Button Content="Refresh" Height="23" HorizontalAlignment="Left" Margin="769,328,0,0" Name="button1"
            VerticalAlignment="Top" Width="75" Click="Button1Click" />
        <DataGrid AutoGenerateColumns="True" Height="310" HorizontalAlignment="Left" Margin="12,12,0,0"
              Name="dataGrid1" VerticalAlignment="Top" Width="832">
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=CounterName}" />
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}" FontStyle="Italic"/>

                                                    <TextBlock><Bold> - Average: </Bold></TextBlock>
                                                    <TextBlock Text="{Binding Converter={StaticResource avgConverter}}" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>

        </DataGrid>

    </Grid>
</Window>

if you need anyother information please let me know.

Just use TextBox and make it readonly, then you can change the TextBox.Style to make it looks like TextBlock.

Perhaps something like this

<TextBox IsReadOnly="True" 
         BorderThickness="0" 
         Background="Transparent" 
         TextWrapping="Wrap" />

使用带有IsReadOnly ="True" TextBox,但也将绑定模式设置为oneWay:

Text="{Binding Converter={StaticResource avgConverter}, Mode=OneWay}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM