c# - 通过具有附加行为的 Thumb 调整弹出窗口的大小

标签 c# wpf attachedbehaviors

如下所示,我有一个弹出窗口,我想用右下角的拇指调整它的大小。拇指有一个附加行为,我想调整大小。

<Popup x:Name="Popbox" Placement="Mouse" StaysOpen="False" Width="50" Height="50" >
    <Grid>
        <Border Background="AliceBlue"/>
        <Thumb HorizontalAlignment="Right" 
               VerticalAlignment="Bottom" 
               Width="16" Height="16" >
            <i:Interaction.Behaviors>
                <helpers:PopupResizeBehaviors PopupObject="{Binding ElementName=Popbox}"/>
            </i:Interaction.Behaviors>
        </Thumb>
    </Grid>
</Popup>


class PopupResizeBehaviors : Behavior<Thumb>
{
    private bool mouseDown;
    private Point oldMousePosition;

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.PreviewMouseLeftButtonDown += (s, e) =>
        {
            mouseDown = true;
        };

        AssociatedObject.DragDelta += (s, e) =>
        {
            if (!mouseDown) return;

            double tempWidth = 0;
            double tempHeight = 0;
            PopupObject.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
            tempWidth = PopupObject.DesiredSize.Width;
            tempHeight = PopupObject.DesiredSize.Height;

            double yadjust = tempHeight + e.VerticalChange;
            double xadjust = tempWidth + e.HorizontalChange;

            PopupObject.Width = xadjust;
            PopupObject.Height = yadjust;
        };

        AssociatedObject.PreviewMouseLeftButtonUp += (s, e) =>
        {
            mouseDown = false;
        };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
    }

    public static readonly DependencyProperty PopupObjectProperty =
        DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null));

    public Popup PopupObject
    {
        get { return (Popup)GetValue(PopupObjectProperty); }
        set { SetValue(PopupObjectProperty, value); }
    }
}

它目前无法正常工作,但应该可以很好地了解我的目标。

如何让这种行为发挥作用?

最佳答案

这是我最后用的。

class PopupResizeBehaviors : Behavior<Thumb>
{
    private const int MaxSize = 500;
    private const int MinSize = 50;

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.DragDelta += (s, e) =>
        {

            Thumb t = s as Thumb;

            if (t.Cursor == Cursors.SizeWE || t.Cursor == Cursors.SizeNWSE)
            {
                PopupObject.Width = Math.Min(MaxSize,
                  Math.Max(PopupObject.Width + e.HorizontalChange,
                  MinSize));
            }

            if (t.Cursor == Cursors.SizeNS || t.Cursor == Cursors.SizeNWSE)
            {
                PopupObject.Height = Math.Min(MaxSize,
                  Math.Max(PopupObject.Height + e.VerticalChange,
                  MinSize));
            }
        };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
    }

    public static readonly DependencyProperty PopupObjectProperty =
        DependencyProperty.RegisterAttached("PopupObject", typeof(Popup), typeof(PopupResizeBehaviors), new UIPropertyMetadata(null));

    public Popup PopupObject
    {
        get { return (Popup)GetValue(PopupObjectProperty); }
        set { SetValue(PopupObjectProperty, value); }
    }
}

关于c# - 通过具有附加行为的 Thumb 调整弹出窗口的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064999/

相关文章:

c# - 在没有xml文件的情况下初始化log4net

c# - 用于 “SecureString”的TypeConverter不支持转换字符串

mvvm - 带有 MVVM Light 的 Windows 应用商店应用程序中的附加行为

wpf - 附加的行为和风格

wpf - 混合行为 - 你能绑定(bind)到它们的属性吗?

Windows 10 通用应用程序中的 C# Ftp 上传?

c# - 如何在 json 请求的方法中路由

c# - 如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?

wpf - 如何设置工具提示样式的InitialShowDelay?

c# - 如何更新由 "code first from database"生成的数据库?