question

StashyCode-7539 avatar image
0 Votes"
StashyCode-7539 asked mm8 rolled back

Drag selecting/partially selecting cell content of DataGridTextColumn WPF DataGrid

I have a DataGrid as such:

 <DataGrid  
     Name="Employees"  
     AutoGenerateColumns="True"   AutoGeneratingColumn="Employees_AutoGeneratingColumn"  
     EnableColumnVirtualization="True"  
     EnableRowVirtualization="True"  
     IsReadOnly="True"  
     ItemsSource="{Binding EmployeesDataTable}"  
     KeyboardNavigation.TabNavigation="Once"  
     SelectionUnit="CellOrRowHeader"   VirtualizingPanel.VirtualizationMode="Recycling" />  

All the autogenerated columns in the DataGrid are DataGridTextColumns.

I want to be able to select just a part of the cell, not the whole cell (so called drag select).

What is the correct approach to that?

EDIT:

I tried the following approach:

  • Setting the IsReadOnly property of the DataGrid to False

  • Setting EditingElementStyle of the DataGridTextColumns:

    <Style TargetType="TextBox">
    <Setter Property="IsReadOnly" Value="True" />
    </Style>

This way, I can select part of the cell content, BUT I have to double-click on it (for it to enter CellEditing mode) and I don't want to do that.

I also tried adding:

 <Style.Triggers>
         <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
         </Trigger>
         <Trigger Property="IsMouseOver" Value="True">
               <Setter Property="IsEditing" Value="True" />
         </Trigger>
  </Style.Triggers>

Which kinda works, but when I move my mouse, the selection is lost, which is pretty annoying.





windows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered StashyCode-7539 commented

The DataGridTextColumn is behaving the same whether it is autogenerated or not. You are setting IsReadOnly = True. This may be one of the reasons why it behaves like you described. You could try to delete IsReadOnly="True" or set IsReadOnly="False".

Update:

 <DataGrid Name="Employees" Height="200"
                AutoGenerateColumns="False" IsReadOnly="True" 
                EnableColumnVirtualization="True" 
                EnableRowVirtualization="True"
                ItemsSource="{Binding EmployeesDataTable}"
               SelectionUnit="CellOrRowHeader"
                VirtualizingPanel.VirtualizationMode="Recycling">
             <DataGrid.Columns>
                 <DataGridTemplateColumn>
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <TextBox IsReadOnly="True" Text="{Binding Name}"/>
                         </DataTemplate>
                     </DataGridTemplateColumn.CellTemplate>
                 </DataGridTemplateColumn>
             </DataGrid.Columns>
         </DataGrid>

If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your input. I tried setting IsReadOnly to False, but I don't want to be able to modify the cell content, I want just to select it.

0 Votes 0 ·

Hi,@StashyCode-7539 . Could you set AutoGenerateColumns="False" and then set the DataTemplate to bind the TextBox? If possible, you can check the updated content in my answer.

0 Votes 0 ·

Unfortunately, all the columns need to be autogenerated. :(

0 Votes 0 ·

Thank you again for the update, but it still won't work in my case. I need the columns to be of type DataGridTextColumn as it derives from DataGridBoundColumn. If I use DataGridTemplateColumn then I won't be able to bind anything correctly. Is there any solution while still using DataGridTextColumn?

0 Votes 0 ·
mm8 avatar image
0 Votes"
mm8 answered mm8 rolled back

If the columns are auto-generated, you could handle the AutoGeneratingColumn event and replace the DataGridTextColumns with DataGridTemplateColumns:

 private void Employees_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
 {
     e.Column = new DataGridTemplateColumn()
     {
         Header = e.Column.Header,
         SortMemberPath = e.PropertyName,
         CellTemplate = XamlReader.Parse("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
         "<TextBox Background=\"Transparent\" IsReadOnly=\"True\" TextWrapping=\"Wrap\" BorderThickness=\"0\" " +
         $"Text=\"{
                 {Binding {e.PropertyName}}}\"/>" +
         "</DataTemplate>") as DataTemplate
     };
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.