Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mvvm

wpf

xaml

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?

like image 795
Takeshi Tokugawa YD Avatar asked Sep 15 '25 11:09

Takeshi Tokugawa YD


2 Answers

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>
like image 200
Sandra Avatar answered Sep 17 '25 01:09

Sandra


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

enter image description here

like image 29
Takeshi Tokugawa YD Avatar answered Sep 17 '25 02:09

Takeshi Tokugawa YD