2

我创建了 Wpf UserControl 并将其托管在 WinForm 中。

<UserControl x:Class="Sapphire.WpfUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="527" Width="992">
<Canvas x:Name="videoCanvas" HorizontalAlignment="Left"  Margin="10,10,0,0" VerticalAlignment="Top" >
    <Label Canvas.ZIndex="2" Content="Label" Canvas.Left="165" Canvas.Top="50" Width="125" Foreground="#FFFFFEFF"/>
    <MediaElement x:Name="videoElement" Canvas.ZIndex="1" Canvas.Left="10" Canvas.Top="10" />
</Canvas>

如设计器文件所示,此 WPF 控件通过 HostElement 托管:

            // 
        // elementHost1
        // 
        this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.elementHost1.Location = new System.Drawing.Point(0, 0);
        this.elementHost1.Name = "elementHost1";
        this.elementHost1.Size = new System.Drawing.Size(1130, 593);
        this.elementHost1.TabIndex = 2;
        this.elementHost1.Text = "elementHost1";
        this.elementHost1.Child = this.wpfUserControl1;

所以看起来一切都是正确的。您还可以看到 DockStyle 是 Fill。但是,WPF 控件不会填充整个 WinForm,并且始终显示为在 Designer 中设置和显示的大小。

我从 Canvas 和 Canvas 包含的 MediaElement 中删除了高度和宽度,但它没有任何效果......

如果有人能指出我在这里做错了什么,我将不胜感激 - 我是 WPF 的新手。

4

1 回答 1

1

您需要删除WidthandHeight以便<UserControl>包含ElementHost控制所包含元素的大小:

<UserControl x:Class="Sapphire.WpfUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">

如果你想在设计器中指定一个特定的尺寸,你可以使用d:DesignHeightd:DesignWidth属性:

<UserControl x:Class="Sapphire.WpfUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" d:DesignHeight="527" d:DesignWidth="992">
于 2016-07-23T09:14:28.283 回答