Winform控件溢出到WPF选项卡之外

时间:2019-03-22 09:46:48

标签: c# wpf winforms winforms-interop

如果WPF TabItem的大小不足以承载Winform控件,则Winform控件将从TabItem中溢出,导致TabControl中的其他一些空格被隐藏,如下所示,“ Winform”按钮从选项卡中溢出:

enter image description here

如何确保Winform控件始终包含在选项卡中?

这是完整的源代码:

MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DockingManagerScrollItem"
        x:Class="DockingManagerScrollItem.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="500">
    <Grid>


        <TabControl Width="450" HorizontalAlignment="Left">
            <TabItem Header="First Tab">
                <local:SingleTab/>
            </TabItem>
        </TabControl>


    </Grid>
</Window>

Winform控件(SimpleThing.designer.cs)

partial class SimpleThing
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.button1.Location = new System.Drawing.Point(0, 0);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(623, 429);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // SimpleThing
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.button1);
        this.Name = "SimpleThing";
        this.Size = new System.Drawing.Size(623, 429);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;
}

WinformWrapper

public class WinformWrapper : WindowsFormsHost
{

private SimpleThing _mapControl;
public WinformWrapper()
{

    _mapControl = new SimpleThing();
    Child = _mapControl;


}

}

SingleTab.xaml

<UserControl x:Class="DockingManagerScrollItem.SingleTab"
             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" 
             xmlns:local="clr-namespace:DockingManagerScrollItem"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Grid ScrollViewer.CanContentScroll="True" ShowGridLines="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="200" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="600" />
                <ColumnDefinition Width="*" />

            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="2" Text="hello" />
            <local:WinformWrapper Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Margin="30" />
        </Grid>
    </ScrollViewer>
</UserControl>

1 个答案:

答案 0 :(得分:1)

WindowsFormsHost的大小设置为托管子控件的大小:

public class WinformWrapper : WindowsFormsHost
{
    private SimpleThing _mapControl;
    public WinformWrapper()
    {
        _mapControl = new SimpleThing();
        Child = _mapControl;
        Width = _mapControl.Width;
        Height = _mapControl.Height;
    }
}

或者看到这个:

ScrollViewer is not working in WPF WindowsFormHost