Takeshi Tokugawa YD
Takeshi Tokugawa YD

Reputation: 979

WPF&MVVM: Library System.Windows.Interactivity in not available anymore?

I need to add System.Windows.Interactivity.dll library via Reference Manager. In Visual Studio 2017, I did not find it. All search results that begins from System.Windows showed on the screenshot below:

enter image description here

I thought that this library becomes build-in in WPF project of current Visual Studio version, so when I added the bellow C# code, no warnings from IDE has been displayed:

private RelayCommand doubleCommand;
public RelayCommand DoubleCommand {
    get {
        return doubleCommand ??
        (doubleCommand = new RelayCommand(obj => {
            Phone phone = obj as Phone;
            if (phone != null) {
                Phone phoneCopy = new Phone {
                    Company = phone.Company,
                    Price = phone.Price,
                    Title = phone.Title
                };
                Phones.Insert(0, phoneCopy);
            }
        }));
    }
}

However, when I added the following XAML markup, it says that Interaction, EventTrigger and InvokeCommandAction has not been found in clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity namespace:

<Window x:Class="MVVM_Tutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVM_Tutorial"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

<!-- ... -->

    <Button Content="2x">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction
                    Command="{Binding DoubleCommand}"
                    CommandParameter="{Binding SelectedPhone}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

<!-- ... -->

</Window>

Has it relationship with System.Windows.Interactivity library or no, what have I do to solve this problem?

Upvotes: 4

Views: 8067

Answers (3)

Sandra
Sandra

Reputation: 708

I discovered that this is now a NuGet package called Microsoft.XAML.Behaviors, after landing on this question based on errors while trying to reference WPF Interactivity incorrectly in Visual Studo 2022. Hope it helps someone else who lands here.

Microsoft open sourced WPF Interactivity, after having available as part of WPF in various ways in the past. See blog article: link

Nuget Package: Microsoft.XAML.Behaviors

XAML Reference: xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

Sample XAML code for binding a Command on TextBox losing focus:

<TextBox Name="txtFilterText" Text="{Binding SmartFilterText}" KeyUp="txtFilterText_KeyUp">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction 
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TextBox}, Path=DataContext.FilterSongsCommand}" 
                CommandParameter="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

Upvotes: 6

mm8
mm8

Reputation: 169330

System.Windows.Interactivity.dll is not included in the .NET Framework but it is a part of the Expression Blend SDK which can be downloaded from here: https://www.microsoft.com/en-us/download/details.aspx?id=10801

Or you could download and reference the assembly using NuGet: https://www.nuget.org/packages/System.Windows.Interactivity.WPF/

Upvotes: 1

Takeshi Tokugawa YD
Takeshi Tokugawa YD

Reputation: 979

Currently, it's required to add System.Windows.Interactivity.WPF via NuGet package manager instead.

enter image description here

Upvotes: 5

Related Questions