<i:Interaction.Behavior>选项未出现用于应用行为。

8

我一直在尝试在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>

但是这不是有效的标签,因为除了System.Windows.Interactivity之外,我可能还需要添加对任何其他程序集的引用。因此,请建议我在XAML中使用标签需要做什么。
1个回答

10

浪费了一个小时后,我才发现只需要将System.Windows.Interactivity作为参考资料并包含进来即可,而这已经是我之前做过的。

问题在于行为无法在任何控件的资源部分中声明。

当我加入如下代码时:

<i:Interaction.Behaviors>

</i:Interaction.Behaviors>

当资源不足时,它可以正常工作。

因此,结论是只需要System.Windows.Interactivity。

永远不要在资源或样式中声明行为部分。


5
截至2019年,参考此链接,请注意命名空间现在更改为http://schemas.microsoft.com/xaml/behaviors(之前是您问题中提到的http://schemas.microsoft.com/expression/2010/interactivity)。 - mins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接