将行为附加到 MetroWindow 失败并导致错误的样式

Attaching Behaviour to MetroWindow fails and results in wrong Style

我有一个简单的测试应用程序,它显示了一个带有附加行为的简单 Mahapps MetroWindow。问题是在附加行为时绘制了 Mahapps MetroWindow 的外边框。

<controls:MetroWindow x:Class="Desktop.Shell.Modern.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:cal="http://www.caliburnproject.org"
        xmlns:modern="clr-namespace:Desktop.Shell.Modern"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Height="350"
        Width="525"
        ResizeMode="CanResizeWithGrip"
        WindowTransitionsEnabled="True"
        ShowIconOnTitleBar="False"
        TitleCaps="False"
        GlowBrush="{DynamicResource WindowTitleColorBrush}" >
    <i:Interaction.Behaviors>
        <modern:SomeBehavior SomeKey="{Binding Key}" />
    </i:Interaction.Behaviors>
    <ContentControl cal:View.Model="{Binding ActiveItem}" />
</controls:MetroWindow>

删除行为后,一切看起来都如预期:

...但是行为本身什么也没做(还)。这是 SomeBehaviour class:

的代码
public sealed class SomeBehavior : Behavior<Window>
{
    public static readonly DependencyProperty SomeKeyProperty =
      DependencyProperty.Register(
        "SomeKey",
        typeof(Key),
        typeof(SomeBehavior),
        new PropertyMetadata(default(Key)));

    public Key SomeKey
    {
        get
        {
            return (Key)this.GetValue(SomeKeyProperty);
        }

        set
        {
            this.SetValue(SomeKeyProperty, value);
        }
    }

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

    }

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

我是不是做错了什么?我应该以不同于将它们附加到 "normal" Windows 的方式附加行为吗?

那是因为 Mahapps.Metro 在 window 样式中设置了其所需的行为,请参阅 MetroWindow.xaml

如果您想附加额外的行为,您必须将这些行为复制到您的window,例如

<controls:MetroWindow x:Class="Desktop.Shell.Modern.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:modern="clr-namespace:Desktop.Shell.Modern"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:Behaviours="http://metro.mahapps.com/winfx/xaml/shared"
    Height="350"
    Width="525"
    ResizeMode="CanResizeWithGrip"
    WindowTransitionsEnabled="True"
    ShowIconOnTitleBar="False"
    TitleCaps="False"
    GlowBrush="{DynamicResource WindowTitleColorBrush}" >
    <i:Interaction.Behaviors>
        <modern:SomeBehavior SomeKey="{Binding Key}" />
        <Behaviours:BorderlessWindowBehavior />
        <Behaviours:WindowsSettingBehaviour />
        <Behaviours:GlowWindowBehavior />
    </i:Interaction.Behaviors>
    <ContentControl cal:View.Model="{Binding ActiveItem}" />
</controls:MetroWindow>