<i:Interaction.Behavior>选项不适用于应用beahviour

Yog*_*esh 6 wpf xaml blend mvvm attachedbehaviors

我一直在尝试在wpf窗口上实现一个行为,因此我在当前的解决方案中添加了对System.Winodws.Interactivity的引用,然后编写了所需的行为.但是为了应用这种行为,我必须在Windows XAML中编写类似的东西.

<Window x:Class="WpfApplication5.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525" 
        xmlns:behav ="clr-namespace:WpfApplication5" 
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity;assembly=System.Windows.Interactivity">
    <Window.Resources>
        <i:Interaction.Behaviors>
            <behav:DialogIconRemoveBehavior></behav:DialogIconRemoveBehavior>
        </i:Interaction.Behaviors>
    </Window.Resources>
    <Grid>

    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

但这不是有效的标签,因为我可能必须添加对System.Windows.Interactivity的任何其他程序集的引用,所以,请建议我在XAML中使用标签时还需要做些什么

Yog*_*esh 8

在浪费了我一个小时后,我才知道只有我必须将System.Windows.Interactivity作为参考包含,而且我已经完成了.

问题是行为不能在任何控件的Resource部分声明.

我采取以下措施的那一刻

<i:Interaction.Behaviors>

</i:Interaction.Behaviors>
Run Code Online (Sandbox Code Playgroud)

在资源之外,它运作良好.

所以,结论只有System.Windows.Interacivity是唯一需要的.

永远不要声明该行为属于资源或风格.

  • 截至 2019 年,请参阅[此](https://devblogs.microsoft.com/dotnet/open-commerce-xaml-behaviors-for-wpf/),并注意命名空间现在为“http://schemas.microsoft.com” /xaml/behaviors`(以前,就像您的问题一样:“http://schemas.microsoft.com/expression/2010/interactivity”) (4认同)