ViewBox内にビデオコントロールを配置することにより、ビデオ比率を維持するWPFプロジェクトがあります。ビデオコントロールは、WindowsFormsHost内にラップされたWinFormsオブジェクトです。また、ViewBoxのサイズを変更するためにgridSplitterを追加しました。奇妙なバグに気付くまで、すべてうまくいきました。gridSplitterコントロールを使用してViewBoxをゼロに近づけると、ビデオの比率が再び拡大された後に失敗します。また、gridSplitterを使用してViewBoxを0まで縮小すると、WindowsFormHostは展開後に完全に消えます(まだ存在しますが、ActualWidthとActualHeightは0になります)。
ViewBoxコントロールを削除し、WindowsFormsHostを使用するだけで問題なく動作しますが、アスペクト比を制御するにはViewBoxが必要です。解決策が見つからない場合は、回避策として自分で比率を制御する必要があります。
私は何か間違ったことをしているに違いないと思ったので、問題を再現する簡単なテストプログラムを作成しました。以下に完全なプログラムへのリンクを投稿します(C#、VS2008にあります)。
テストプログラムでは、サイズを変更できるグリッドスプリッターを備えた2列のグリッドを作成します。次に、左側のコントロールにViewBoxを配置し、その中にWindowsFormsHostを配置します。次に、WindowsFormsHost内で赤い_winFormsButtonをホストします。
左の列を完全に縮小してから再度展開するまで、ボタンは細かく拡大縮小されます。ボタンがなくなりました。これを防ぐにはどうすればよいですか。左の列を縮小および拡大した後もボタンは拡大縮小され続けますか?ありがとう。
XAML:
<Window x:Class="DisappearingHost.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="0.5*" />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" HorizontalAlignment="Left" Name="gridSplitter1" Width="10" VerticalContentAlignment="Center" />
<Viewbox>
<WindowsFormsHost Name="windowsFormsHost1" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Viewbox>
</Grid>
</Window>
コードビハインド:
public partial class Window1 : Window
{
System.Windows.Forms.Button _winFormsButton = new System.Windows.Forms.Button();
public Window1()
{
InitializeComponent();
_winFormsButton.Text = "I disappear!";
_winFormsButton.Size = new System.Drawing.Size(50, 50);
_winFormsButton.BackColor = System.Drawing.Color.Red;
windowsFormsHost1.Child = _winFormsButton;
}
}
サンプルコード出力:
テストプロジェクトコード(VS2008)へのリンク: コード