Pergunta

I have Scrollviewer which contains a frame with a WindowsFormsHost. The WindowsFormsHost contains a DataGridView (please don't ask why I'm not doing this with a WPF DataGrid Control).

Because the DataGridView causes display errors while scrolling with the scrollviewer I disabled the scrollviewer and enabled the scrolling on my DataGridView.

    <Grid x:Name="LayoutRoot">
        <WindowsFormsHost HorizontalAlignment="Stretch" Name="_windowsFormsHostGrid" VerticalAlignment="Stretch">
            <Win.Grid:DataGridView x:Name="_buchungGrid" ScrollBars="Both" BorderStyle="None" BackgroundColor="#F7F8FA" CellFormatting="_gridBuchungen_CellFormatting" SelectionChanged="GridSelectionChanged" DoubleClick="_buchungInovaGrid_DoubleClick" AutoSize="True" AutoColumnWidthMode="Window" ZebraColor="LightGray" Anchor="Left" Dock="Fill" />
        </WindowsFormsHost>
    </Grid>

This seems to work. As long I don't resize the Window. When I resize the window (and this will cause all child elements to resize including scrollviewer, frame and WindowsFormsHost), the scrollbars of my DataGridView disappears and I'm not longer able to scroll my grid. I can resize to the old size of the window, but the scrollbars are still hidden.

Any idea why this happens and how to fix it? I'm also not sure why they disappear because I'm resizing just one pixel and this occurs.

Foi útil?

Solução

There seems not to be a solution for this problem. I ended up by using a WPF DataGrid and extend it by the functionality I need.

WinForms will be painted all the time at the top of all other elements. The only solution seems to wrap a windows around it to get scrolling fixed but this would ugly as hell.

Outras dicas

Given a Forms.DataGridView (dgv) inside a Forms.UserControl (myUserControl) inside a WindowsFormsHost, I discovered that the DGV was given larger dimensions than the UC, so the scrollbars were not visible. (If UC is instead in a WinForm, scrollbars appear as expected; there seems to be an issue with resize logic inside WFHost.)

I was able to fix this in my UC's SizeChanged handler:

// VB code:
Public Class MyUserControl
...
Private Sub MyUserControl_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        ' dgv.Size is much larger than it should be; not sure why.
        ' my dgv has controls above it, but extends all the way to bottom and right;
        ' if yours does not, then subtract more as needed.
        dgv.Size = New Size(Me.Size.Width - dgv.Left, Me.Size.Height - dgv.Top)
End Sub
...
End Class

The result is that the WinForms drawing stays within the area it is supposed to; the scrolling is done in WinForms. (NOT using a WPF scrollviewer.)


XAML for WPF:

...
xmlns:mywf="clr-namespace:MyWinFormAssembly;assembly=MyWinFormAssembly"
...

<WindowsFormsHost>
    <mywf:MyUserControl />
</WindowsFormsHost>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top