开发者

How do I make a WPF popup draggable using a thumb?

I've searched long and hard to find an example of this on the net. I've seen the alternative methods for this but I want to specifically use a thumb. In trying to hash out a solution I've been unable to get the functionality right/working as I'm fairly new to WPF. I'd be grateful if someone could give an example of how开发者_StackOverflow社区 you may implement the above.

Cheers


A Popup doesn't give you an obvious way to move it; you can only place it relative to its placement target. However it does give you and additional offset relative to the placement target and that's enough to move it anywhere.

Here's a working markup-only solution that demonstrates a draggable Popup using a Thumb using Markup Programming. There is a text box that has a popup that pops up when the text box receives the keyboard focus. The popup has some text and a thumb.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="http://markupprogramming.codeplex.com/markup/programming"
    Height="300" Width="300">
    <Grid>
        <StackPanel>
            <TextBox x:Name="textBox1" Width="200" Height="20"/>
        </StackPanel>
        <Popup Name="popup" PlacementTarget="{Binding ElementName=textBox1}"
               IsOpen="{Binding IsKeyboardFocused, ElementName=textBox1, Mode=OneWay}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Background="White" Text="Sample Popup content."/>
                <Thumb Name="thumb" Width="20" Height="20"/>
            </StackPanel>
            <p:Attached.Operations>
                <p:ScriptHandler Path="@FindElement('thumb').DragDelta">
                    @AssociatedObject.HorizontalOffset += @EventArgs.HorizontalChange;
                    @AssociatedObject.VerticalOffset += @EventArgs.VerticalChange;
                </p:ScriptHandler>
            </p:Attached.Operations>
        </Popup>
    </Grid>
</Window>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜