WPF 〜 WindowsForms 相互運用の問題点(その2)
最近いろいろ試してて気が付いたのですが、WPF で WindowsFormsHost コントロールを使って WindwosForms のコントロールを配置すると描画処理で問題が起こることが判りました。
再現手順
以下のように、ScrollViewer の中に StackPanel を配置。StackPanel の Orientation プロパティを Horizontal にし、Button と WindowsFormsHost を交互に配置します。WindowsFormsHost には WindowsForms の Button を配置します。
色はわかりやすいよう、WPF の Button が青、Forms の Button を赤にしました。
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:win="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title="MainWindow" Height="400" Width="400"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="50" /> </Grid.ColumnDefinitions> <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <StackPanel Height="350" Orientation="Horizontal" > <Button Content="Button" Width="40" Background="Blue" /> <WindowsFormsHost Width="40"> <win:Button Dock="Fill" BackColor="Red" /> </WindowsFormsHost> <Button Content="Button" Width="40" Background="Blue" /> <WindowsFormsHost Width="40"> <win:Button Dock="Fill" BackColor="Red" /> </WindowsFormsHost> <Button Content="Button" Width="40" Background="Blue" /> <WindowsFormsHost Width="40"> <win:Button Dock="Fill" BackColor="Red" /> </WindowsFormsHost> <Button Content="Button" Width="40" Background="Blue" /> <WindowsFormsHost Width="40"> <win:Button Dock="Fill" BackColor="Red" /> </WindowsFormsHost> <Button Content="Button" Width="40" Background="Blue" /> <WindowsFormsHost Width="40"> <win:Button Dock="Fill" BackColor="Red" /> </WindowsFormsHost> </StackPanel> </ScrollViewer> </Grid> </Window>
起動するとこうなります。
Window のサイズを小さくしてください。WPF の Button は ScrollViewer の領域内に収まりますが、Forms の Button は ScrollViewer の領域からはみだして表示してるのが判ると思います。これは WPF と Forms の描画ロジックが全く違うために起こるようで、対処法もないみたいです(汗