如何调整弹出窗口的大小以适应正确的方向?

问题描述 投票:0回答:1

我有一个弹出窗口,它的大小是动态变化的,以适应内容。当大小增加时,弹出窗口的宽度只向左增加,直到它到达窗口的左边缘。但在我的例子中,我希望弹出窗口的宽度向右增加。

以下是我使用的代码片段

XAML:

<StackPanel>
        <TextBox x:Name="popupText" Margin="5"/>
        <Button Content="Change PopupText" Click="Button_Click_1" Margin="5"/>
        <Canvas x:Name="canvas" Width="200" Height="200" Background="Red">
            <Rectangle x:Name="rect" Canvas.Top="50" Canvas.Left="50" 
           Width="50" Height="100"
           Stroke="White" StrokeThickness="3"/>
            <Popup x:Name="popup" AllowsTransparency="true"
                   IsOpen="True" Placement="Relative"
                   PlacementTarget="{Binding ElementName=canvas}"
                   PlacementRectangle="50,50,50,100"
                   HorizontalOffset="0"
                   >
                <TextBlock x:Name="textBlock" FontSize="14" Background="Yellow"
             TextWrapping="Wrap" >
    This is a popup with a PlacementRectangle.
                </TextBlock>
            </Popup>
        </Canvas>
</StackPanel>

C#:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    this.textBlock.Text += this.popupText.Text;
}

ScreenShots:

改变大小之前

enter image description here

改变尺寸后

enter image description here

在文本框中输入任何内容,然后点击 "更改弹出式文本 "按钮,你可以清楚地看到弹出式窗口的大小向左增加。

以上是弹出窗口的一种行为吗?我可以改变这个行为,并增加弹出窗口的宽度在正确的方向?

先谢谢你,Shobika。

wpf popup resize wpf-controls
1个回答
0
投票

你可能想看看 自定义弹出式放置.

设置 Placement="Custom" 在您的弹出式菜单上,使用 CustomPopupPlacementCallback 属性来引用一个回调。在回调中,您可以计算出相对于您的放置目标的首选放置。

© www.soinside.com 2019 - 2024. All rights reserved.