Search code examples
wpfdatagridvisualbrush

Datagrid Column Dragging Indicator is clipped


Edit: Pavel has shown that it may not be VisualBrush's fault so I have renamed the question for my specific problem.

The example is a WPF Datagrid; Scroll right until a column header is partially visible. drag the partially visible column header to reorder it. the drag indicator is the partial view of the header (not complete)

My Solution - Still interested in other solutions
I subscribe to ColumnReordering and replace the drag indicator with my own. Copying the source of DataGridColumnFloatingHeader for my own class. I change the line that creates the visual brush using the header, to a line that creates the visual brush using the first child of the header (which is not clipped) remove the offset and use 0,0..


Solution

  • Edit

    I believe it is a bug in DataGrid implementation. I suggest you scroll on column start to avoid such unwanted behaviour if it really treats you.

    IMHO problem itself is in System.Windows.Controls.DataGridColumnFloatingHeader. Due this class is internal it is hard to workaround the problem.

    How to reproduce:

    Scroll a bit right, than drag first column.

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="400" Width="300">
    
        <ScrollViewer x:Name="uiScroll">
            <DataGrid Grid.Row="3" x:Name="uiGrid">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="200" Header="Test 1" Binding="{Binding Key}" />
                    <DataGridTextColumn Width="200" Header="Test 2" Binding="{Binding Value}" />
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>
    
    </Window>
    

    ...

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            uiGrid.ItemsSource = new Dictionary<string, string>() { { "key1", "val1" }, { "key2", "val2" } };
        }
    
    }