Viewbox使WindowsFormsHost在WPF中消失 [英] Viewbox makes WindowsFormsHost disappear in WPF

查看:190
本文介绍了Viewbox使WindowsFormsHost在WPF中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF项目,通过将视频控件放置在ViewBox中来维护视频比例.视频控件是包装在WindowsFormsHost中的WinForms对象.我还添加了gridSplitter来调整ViewBox的大小.一切都很好,直到我注意到一个奇怪的错误.当我使用gridSplitter控件将ViewBox最小化到接近于零时,在再次扩展视频比例后,视频比例会变大.当我使用gridSplitter将ViewBox缩小到0时,WindowsFormHost在扩展后会完全消失(它仍然存在,但是它的ActualWidth和ActualHeight现在为0).

I have a WPF project where I maintain video ratio, by placing video control inside a ViewBox. The video control is a WinForms object wrapped inside a WindowsFormsHost. I also added a gridSplitter to resize the ViewBox. It all worked great, until I noticed a strange bug. When I would use the gridSplitter control to minimize the ViewBox close to zero, the video ratio would get screwed up after expanding it again. And when I used gridSplitter to contract the ViewBox all the way down to 0, the WindowsFormHost would completely disappear after expanding (it's still there, but it's ActualWidth and ActualHeight are now 0).

删除ViewBox控件,仅使用WindowsFormsHost可以很好地工作,但是我需要使用ViewBox来控制纵横比.如果找不到解决方案,则必须自己控制该比率,以作为解决方法.

Removing the ViewBox control, and just using WindowsFormsHost works fine, but I need the ViewBox to control the aspect ratio. If I can't find a solution, I will have to control the ratio myself as a workaround.

我认为我必须做错了什么,所以我写了一个快速测试程序来重现该问题.我将在下面发布指向完整程序的链接(在C#中,VS2008).

I figured I must be doing something wrong, so I wrote a quick test program that reproduces the problem. I will post a link to the complete program below (it's in C#, VS2008).

在测试程序中,我创建了一个带有两列的网格,并带有一个网格拆分器,可让您调整它们的大小.然后,我将ViewBox放在左侧控件中,并在其中放置WindowsFormsHost.然后,我在WindowsFormsHost中托管一个红色的_winFormsButton.

In the test program I create a grid with two columns with a gridsplitter that lets you resize them. I then place a ViewBox in the left control and place a WindowsFormsHost inside it. I then host a red _winFormsButton inside the WindowsFormsHost.

该按钮可以正常缩放,直到完全收缩左列,然后再次将其展开.该按钮不见了.如何防止这种情况发生,以便按钮在缩小和扩展左列后继续缩放?谢谢.

The button scales fine, until you completely contract the left column and then expand it again. The button is gone. How do I prevent that from happening, so the button continues to scale after contracting and expanding left column? Thanks.

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): 代码

Link to test project code (VS2008): code

推荐答案

我最终摆脱了Viewbox,自己控制了WidowsFormHost的尺寸.

I ended up getting rid of the Viewbox, and controlling WidowsFormHost dimensions myself.

这篇关于Viewbox使WindowsFormsHost在WPF中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆