编程语言
首页 > 编程语言> > c# – 将行为附加到MetroWindow失败并导致错误的样式

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

作者:互联网

我有一个简单的测试应用程序,它显示了一个带有附加行为的简单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类的代码:

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();
    }
}

难道我做错了什么?我应该以不同于将其附加到“普通”Windows的方式附加行为吗?

解决方法:

这是因为Mahapps.Metro在窗口样式中设置了所需的行为,请参阅MetroWindow.xaml.

如果要附加其他行为,则必须将这些行为复制到窗口,例如

<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>

标签:c,wpf,mahapps-metro
来源: https://codeday.me/bug/20190624/1275868.html