Resize a popup in WPF


Popup control has not build in support for resize. But we can do this by ourself. The following code,

Code Snippet

<Window

   x:Class=”ForumProjects.MainWindow”

   xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

   xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

   Title=”MainWindow” Height=”700″ Width=”800″>

    <StackPanel>

        <Button Click=”ShowPopup”>

            <Grid>

                <TextBlock>Show Popup</TextBlock>

                <Popup Name=”Popup” StaysOpen=”False” Width=”100″ Height=”100″>

                    <Grid Background=”Blue”>

                        <Thumb HorizontalAlignment=”Right”VerticalAlignment=”Bottom” Width=”16″ Height=”16″

                              DragStarted=”onDragStarted”DragDelta=”onDragDelta” DragCompleted=”onDragCompleted”/>

                    </Grid>

                </Popup>

            </Grid>

        </Button>

    </StackPanel>

</Window>

namespace ForumProjects

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void ShowPopup(object sender, RoutedEventArgs e)

        {

            this.Popup.IsOpen = true;

        }

        private void onDragStarted(object sender, DragStartedEventArgs e)

        {

            Thumb t = (Thumb)sender;

            t.Cursor = Cursors.Hand;

        }

        private void onDragDelta(object sender, DragDeltaEventArgs e)

        {

            double yadjust = this.Popup.Height + e.VerticalChange;

            double xadjust = this.Popup.Width + e.HorizontalChange;

            if ((xadjust >= 0) && (yadjust >= 0))

            {

                this.Popup.Width = xadjust;

                this.Popup.Height = yadjust;

            }

        }

       private void onDragCompleted(object sender, DragCompletedEventArgs e)

        {

            Thumb t = (Thumb)sender;

            t.Cursor = null;

        }

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s