置百丈玄冰而崩裂,掷须臾池水而漂摇。

WPF中使用WinformsHost显示滚动条的问题

C# 强强

在WPF项目中如果使用了winfrom控件,如果带了滚动条,winfrom控件永远在WPF的控件上面,覆盖了WPF的控件,感觉想浮动了一样

解决方法:就是在ScrollViewer上嵌套一层ElementHost,其作用相当于将ScrollViewer整一层又包装成Winform。 

代码如下

 <xfi:WindowsFormsHost>
            <xfi:ElementHost>
                <ScrollViewer>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Button Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="75" Click="Button_Click"/>
                        <xfi:WindowsFormsHost  Grid.Column="1" Margin="5"   >
                            <xf:RichTextBox x:Name="rt1"></xf:RichTextBox>
                        </xfi:WindowsFormsHost>
                        <WindowsFormsHost    Grid.Column="1" Margin="5"  Grid.Row="1">
                            <xf:RichTextBox x:Name="rt2"></xf:RichTextBox>
                        </WindowsFormsHost>
                    </Grid>
                </ScrollViewer>
            </xfi:ElementHost>
        </xfi:WindowsFormsHost>

这样就可以了。不过还有一个问题,就是界面里面的WPF控件绑定会没有效果