Shobika Palani
Shobika Palani

Reputation: 21

How to resize popup in right direction?

I have a popup for which the size changes dynamically to fit the content. When the size increases, popup width increased in left direction only until it reaches left edge of the window. But in my case, I want popup width to be increased in right direction.

Below is the code snippet I am using

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:

Before changing size

enter image description here

After changing size

enter image description here

Type anything in the textbox and click "Change PopupText" button. you can clearly see popup size increasing in left direction.

Is above one behavior of popup? Could I change this behavior and increase popup width in right direction?

Thanks in Advance, Shobika.

Upvotes: 1

Views: 1122

Answers (1)

Ravigneaux
Ravigneaux

Reputation: 68

You may want to have a look at custom popup placements.

Set Placement="Custom" on your popup and use the CustomPopupPlacementCallback property to reference a callback. In the callback, you can calculate preferred placements, relative to your placement target.

Upvotes: 1

Related Questions