1

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.

  • The direction of the `Popup` is determined by an OS-level "handedness" setting. On devices with any kind of pen/tablet support, this will make Popups open on the left-hand side and expand in that direction by default. Here is a SO issue which talks about it as well as a way for you to override it: https://stackoverflow.com/questions/18113597/wpf-handedness-with-popups – Keithernet May 06 '20 at 15:13

1 Answers1

1

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.

Ravigneaux
  • 68
  • 7