StickWindow在Mahapps

时间:2015-03-01 17:50:29

标签: wpf mahapps.metro

我试图实现对边缘的捕捉。我找到了一个答案here并实施了,但它破坏了Mahapps MetroWindow的无铬窗口。

我已经调整了使用行为的解决方案,因此我只想分享行为snnipet(你可以找到StickyWindow代码here):

用法

<metro:MetroWindow
x:Class="Communicator.Main.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:cal="http://www.caliburnproject.org"
xmlns:phoneView="clr-namespace:Communicator.Softphone.Views;assembly=Communicator.Softphone"
xmlns:converters="clr-namespace:Communicator.ControlLibrary.Converters;assembly=Communicator.ControlLibrary"
xmlns:local="clr-namespace:Communicator.Main"
Title="Comunicador" Height="600" Width="450"
WindowStartupLocation="CenterScreen" ResizeMode="NoResize" TitlebarHeight="40"
EnableDWMDropShadow="True" WindowTransitionsEnabled="False"
cal:Message.Attach="[Event KeyDown] = [Action KeyDown($executionContext)]; [Event Activated] = [Action ViewActivated]; [Event Deactivated] = [Action ViewDeactivated]">
<i:Interaction.Behaviors>
    <local:StickyWindowBehavior />
</i:Interaction.Behaviors>

StickWindowBehavior

public class StickyWindowBehavior : Behavior<Window>
{
    private StickyWindow stickWindow = null;

    protected override void OnAttached()
    {
        base.OnAttached();
        stickWindow = new StickyWindow(AssociatedObject)
        {
            StickToScreen = true,
            StickOnResize = true,
            StickOnMove = true
        };
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        if (stickWindow != null)
            stickWindow.ReleaseHandle();
    }
}

我的MetroWindow会发生什么:

enter image description here

如何在不丢失MetroWindow的无边框窗口的情况下添加操纵杆行为?

1 个答案:

答案 0 :(得分:2)

您的问题原因与Attaching Behaviour to MetroWindow fails and results in wrong Style中的原因相同: Mahapps.Metro 在窗口样式中设置其行为,因此实际上您将覆盖XAML中的声明。< / p>

您必须设置标准行为和您自己的行为:

<i:Interaction.Behaviors>
    <local:StickyWindowBehavior />
    <Behaviours:BorderlessWindowBehavior />
    <Behaviours:WindowsSettingBehaviour />
    <Behaviours:GlowWindowBehavior />
</i:Interaction.Behaviors>