8

I have been trying to implement a behavior on a wpf window therefore I have added reference to System.Winodws.Interactivity in my current solution and then wrote the desired behavior. but in order to apply this behavior, I have to write something like this in 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>

but this is not valid tag because I might have to add reference to any other assembly apart form System.Windows.Interactivity, So, please suggest what else I have to do in order to use tag in XAML

Styxxy
  • 7,462
  • 3
  • 40
  • 45
Yogesh
  • 3,044
  • 8
  • 33
  • 60

1 Answers1

10

After wasting my one hour I came to know that only I had to include System.Windows.Interactivity as reference and which I had already done.

the issue was that Behaviors can not be declared in the Resource part of any control.

the moment I took the following

<i:Interaction.Behaviors>

</i:Interaction.Behaviors>

out of the resources then it worked fine.

So, conclusion only System.Windows.Interacivity is the only required.

Never declare that behavior part in the resources or style.

Yogesh
  • 3,044
  • 8
  • 33
  • 60
  • 4
    As of 2019, see [this](https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/) and note the namespace is now `http://schemas.microsoft.com/xaml/behaviors` (previously, like in your question: `http://schemas.microsoft.com/expression/2010/interactivity`) – mins Sep 24 '19 at 16:39