Winforms / WPF互操作-调整嵌套用户控件的大小

时间:2018-10-18 10:55:13

标签: c# wpf winforms-interop

我有一个winforms用户控件,其中包含一个元素主机,该元素主机本身包含一个wpf用户控件,其中包含一个文本框。

似乎我没有重新调整最里面的文本框的大小。理想情况下,它将调整大小以填充elementhost,并在调整大小时自行调整大小以填充winforms用户控件。

winforms用户控件具有以下构造函数代码

public partial class TextBox : UserControl, ITextBox
{
    private System.Windows.Forms.Integration.ElementHost _textBoxHost;
    private TextBoxExViewModel _viewModel;
    private TextBoxEx _textBoxEx;

    public TextBox()
    {
        InitializeComponent();

        this._textBoxHost = new System.Windows.Forms.Integration.ElementHost();           
        this._textBoxEx = new IfxNetControls.TextBoxEx();
        this._viewModel = new TextBoxExViewModel();

        _textBoxEx.DataContext = _viewModel;

        this.SuspendLayout();

        // set up wpf host elementHost1             
        this._textBoxHost.Dock = System.Windows.Forms.DockStyle.Fill;
        this._textBoxHost.Location = new System.Drawing.Point(0, 0);
        this._textBoxHost.Name = "textBoxHost";
        this._textBoxHost.Size = new System.Drawing.Size(340, 245);
        this._textBoxHost.TabIndex = 0;
        this._textBoxHost.Text = "textBoxHost";
        this._textBoxHost.AutoSize = false;
        this._textBoxHost.ChildChanged += new System.EventHandler<System.Windows.Forms.Integration.ChildChangedEventArgs>(this.ChildChanged);
        this._textBoxHost.Child = this._textBoxEx;
        //this._elementHost1.Child = this._wpfTextBox;

        // set up usercontrol textbbox 
        this.Controls.Add(this._textBoxHost);
        this.Name = "TextBox";
        this.Size = new System.Drawing.Size(340, 245);
        this.AutoSize = false;

        //
        this.ResumeLayout(false);
    }

    ...

请注意Dock属性设置为Fill

我还在Winforms用户控件中尝试过调整大小处理程序

private void TextBox_Resize(object s, EventArgs e)
{                
    this._textBoxEx.Width = this._textBoxHost.Width;
    this._textBoxEx.Height = this._textBoxHost.Height; 
}

因为当我跟踪高度和宽度时,wpf文本框始终较小

WinformsUserControl: 208,35
ElementHost: 208,35
WpfUsercontrol: 181.527272727273,30.5454545454545

这似乎在使用时有所体现(请参阅下面的img 3)-尽管我确实想知道所有3个控件的度量单位是否相同。

wpf用户控件xaml看起来像这样

<UserControl x:Class="IfxNetControls.TextBoxEx"
         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:..."             
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch"
         HorizontalContentAlignment="Stretch"
         VerticalContentAlignment="Stretch"
         Height="Auto"
         Width="Auto"
         Margin="0"
         Padding="0"
         >
...

,文本框xaml的“高度”和“宽度”设置为“自动”。我还尝试将“水平/垂直对齐方式”和“ ContentAlignment”设置为“拉伸”。

显示时,它最初看起来像这样。 (我放置了2个相同的尺寸以显示更改)

enter image description here

最初,最上面的一个具有焦点,但是如果我将焦点移开,它会调整为看起来像文本的大小(即使我已将Autosize显式设置为false)。

enter image description here

如果我弄乱了背景色,它看起来像这样,这似乎表明了文本框与其容器的大小不同,底部和左侧有空白

enter image description here

理想情况下,所有控件的尺寸都相同(底色也相同!)。

我对wpf不太熟悉,所以我想知道是否有人可以指出我的错误。

EDIT-1:所以我仍在尝试找出问题所在!

如果我更新winforms用户控件的背景色并且不更改XAML用户控件或文本框的背景,则当文本框没有没有焦点

时,它看起来像这样

enter image description here

但是,当它获得焦点时,看起来wpfusercontrol / textbox会展开以填充winforms usercontrol容器-因此:

enter image description here

,然后失去焦点时,它将返回到其先前的大小,从而再次显示winforms用户控件的背景。

我不太了解,因为当我跟踪elementhost的宽度和高度时,它的大小与winforms用户控件相同。我已经给wpf用户控件提供了它自己的背景色,但是我从没想到它表明文本框实际上正确地填充了wpf用户控件。似乎wpf usercontrol / textbox的大小在我不期望的地方调整。

这是预期的行为吗?

再次感谢。

2 个答案:

答案 0 :(得分:0)

在WPF中,您通常使用StylesTemplatesCustom Controls。这些都有很多教程,我建议您看一看。 UserControls不如这些灵活,通常用于创建不需要那么灵活的大型UI。例如创建一个表格。

  

理想情况下,所有控件的尺寸都相同(底色也相同!)。

在这种情况下,我建议您使用Style

在此说明Auto*的工作方式以及如何使用Style

<Window x:Class="WpfApp1.MainWindow"
    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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <Style TargetType="{x:Type TextBox}"> <!-- Style for all Textboxes on this window -->
        <Setter Property="TextAlignment" Value="Left"/>
        <Setter Property="Width" Value="180"/>
        <Setter Property="Margin" Value="5"/> <!-- the same as 5,5,5,5  left,top,right,bottom  -->
        <Setter Property="Background" Value="Blue"/>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition  Width="2*"/><!-- It's 2 times wider than the other colums with * -->
        <ColumnDefinition Width="Auto"/> <!-- the Column width is based on the widest control in this column -->
        <ColumnDefinition /><!-- you can also write Width="*" -->
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/> <!-- you can also write Height="*" -->
        <RowDefinition Height="Auto"/> <!-- the Row's Height is based on the highest control in this row -->
        <RowDefinition Height="Auto"/> <!-- the Row's Height is based on the highest control in this row -->
        <RowDefinition Height="Auto"/> <!-- the Row's Height is based on the highest control in this row -->
        <RowDefinition Height="Auto"/> <!-- the Row's Height is based on the highest control in this row -->
        <RowDefinition/> <!-- you can also write Height="*" -->
    </Grid.RowDefinitions>
    <Grid Grid.Row="1" Grid.Column="1" Background="red"> <!-- Just to have a container. In your example this is the UserControl -->
        <TextBox />
    </Grid>
    <TextBox Grid.Row="2" Grid.Column="1"/>
    <TextBox Grid.Row="3" Grid.Column="1"/>

</Grid>

答案 1 :(得分:0)

原来这是一个扩展问题。

我在基础操作系统(win7)上进行了自定义文本缩放(115%),并且在将用户控件绘制到旧式COM用户控件时,wpf用户控件文本框的大小变得太小。如果我将自定义缩放比例重置为100%,则一切正常。

相关问题