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:
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?
Currently, it's required to add System.Windows.Interactivity.WPF via NuGet package manager instead.
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/
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>
Related
We encountered an interesting behavior on .Net 4.5 (4.6.2 also tested).
The project has multiple plugin dlls.
main exe will load DataTemplates (view) and ViewModels from DLLs using MEF.
if StepView and StepVm and main frame code are in one project (not using MEF), The 2 buttons I show below are working.
if move StepView and StepVm to plugin dll, only second button will work. First one shows binding error in output console. need to talk to manager if I can post error msg here, just wpf standard binding error.
Can anyone share some insights here?
Thanks.
StepView
<UserControl
x:Class="StepView"
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:local="clr-namespace:ScriptHighlighter"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignInstance local:StepVm}"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<ItemsControl x:Name="XItemsControl" ItemsSource="{Binding Names}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button
Content="Not Wokring in plugin mode"
Command="{Binding ElementName=XItemsControl, Path=DataContext.DeleteCommand}"
CommandParameter="{Binding}" />
<Button
Content="Wokrs in plugin mode"
Command="{Binding Path=DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}, Mode=FindAncestor}}"
CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
StepVm
public class StepVm:ViewModelBase
{
public StepVm()
{
this.Names = new List<string>(){"1", "2", "3"};
}
public List<string> Names { get; set; }
public ICommand DeleteCommand => new RelayCommand<string>(n =>
{
Debug.WriteLine($"logic to delete {n}");
});
}
Because MEF loads your UserControl dynamically into the Visual Tree, you are likely to have issues with NameScope, which I think is whats happening here.
WPF XAML Namescopes
To be honest, your use of ElementName binding is problematic, because your are in a DateTemplate which is an encapsulation boundary, so although it works outside MEF its not a typically supported scenario.
I am making some wpf application and I need option for changing languages. I have folder named Resorces in my solution, where I store all my resx file - actually language.resx and language.en-EN.resx. My XAML looks like:
<Window x:Class="CoinCatalogue.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:CoinCatalogue"
xmlns:resx="clr-namespace:CoinCatalogue.Resources"
mc:Ignorable="d"
Title="{x:Static resx:language.WindowName}" Height="480" Width="640">
<Grid>
<Menu x:Name="menu" HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="517">
<MenuItem x:Name="File" Header="{x:Static resx:language.File}">
<MenuItem x:Name="Open" Header="{x:Static resx:language.Open}"/>
</MenuItem>
</Menu>
</Grid>
Everything is ok - I have access to string in app. And in my main class I am trying to change culture with line:
language.Culture = new System.Globalization.CultureInfo("en-EN");
But nothings happen to my app. This line is before InitializeComponent();.
What am I doing wrong? I am using Visual Studio 2015. /is it a good idea to change app language in this way?
I think how you are changing the culture is the issue.
I just put a quick sample together where I had the following resx files:
Resources.en-US.resx
Resources.fr-FR.resx
Resources.resx
If I run the app without attempting to change the culture, I get the strings from the Resources.en-US.resx file as that is my default. If I add this code before InitializeComponent my strings come from the Resources.fr-FR.resx file:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-FR");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-FR");
I have encountered a rather strange problem using the GalaSoft - MVVM Light Toolkit in a Windows Phone 8 project.
Suddenly (after merging some stuff) all my EventToCommand calls are not working anymore. They worked before. I already tried to remove the MvvmLight toolkit and reference it again with Nuget, but the result stays the same.
One example:
MainMenuPage.xaml
<phone:PhoneApplicationPage
...
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
....>
<phone:PhoneApplicationPage.DataContext >
<Binding Source="{StaticResource MainMenuViewModel}"/>
</phone:PhoneApplicationPage.DataContext>
<!-- catch back key press -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="BackKeyPress">
<commands:EventToCommand
Command="{Binding BackKeyPressCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
MainMenuViewModel.cs
// back key press command
public RelayCommand<object> BackKeyPressCommand { get; set; }
public MainMenuViewModel()
{
BackKeyPressCommand = new RelayCommand<object>(
BackKeyPress,
(o) => true
);
...
}
private void BackKeyPress(Object o)
{
// handle back key press
}
This worked perfectly before, but now the BackKeyPress(Object o) Method never gets called anymore.
This happens for all EventToComamnd calls.
If I remove the xmlns tags, Resharper suggests to add this:
xmlns:command="http://www.galasoft.ch/mvvmlight"
Result of that:
The name "EventToCommand" does not exist in the namespace "http://www.galasoft.ch/mvvmlight"
Anyone encountered a similar problem or has an idea what could have caused this?
These namespaces are correct.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:commands="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
But your PhoneApplicationPage has wrong DataContext.
DataContext="{Binding Path=MainMenuViewModel, Source={StaticResource Locator}}"
MainMenuViewModel is property in the ViewModelLocator:
public MainMenuViewModel MainMenuViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainMenuViewModel>();
}
}
Btw the argument of BackKeyPress is CancelEventArgs. You should use:
public RelayCommand<CancelEventArgs> BackKeyPressCommand { get; private set; }
this.BackKeyPressCommand = new RelayCommand<CancelEventArgs>(this.BackKeyPress)
private void BackKeyPress(CancelEventArgs e)
{
}
Windows Phone 8.1
Windows 8.1 Behavior SDK: How to use InvokeAction with InputConverter to pass arguments to a Command
Microsoft has developed it's own EventToCommand functionality. It's located in Behaviors SDK. Someone on stackoverflow told to get this SDK via Nuget. If you can't find the package in NuGet - get it in Add reference dialog.
(My add reference dialog may differs from original because of Productivity Power Tools extension)
Here is example of simple usage:
<ListBox ItemsSource="{Binding Persons, Mode=OneWay}"
SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding DisplayPersonCommand}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ListBox>
I have recently switched to Visual Studio Premium 2012, and discovered a XAML designer issue while using MVVM Light (v. 4.0.23.4) on a Silverlight 5 project. Here is the snippet of code that triggers the issue:
<sdk:AutoCompleteBox Text="{Binding Path=SomeProperty, Mode=TwoWay}" ItemsSource="{Binding Path=SomeCollectionProperty}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<mvvmlight:EventToCommand Command="{Binding Path=SomeRelayCommand}" CommandParameter="{Binding SomeParameter}" />
</i:EventTrigger>
</i:Interaction.Triggers>
The relevant namespaces are:
xmlns:mvvmlight="http://www.galasoft.ch/mvvmlight"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
This gives me an "Invalid Markup" error on the designer view and a "A value of type 'EventToCommand' cannot be added to a collection or dictionary of type 'TriggerActionCollection' error on the EventToCommand line in the xaml. This does not happen in VS2010.
Any ideas on how to fix this issue?
Thanks in advance!
Alyce solve the problem. The problem was the system.windows.interactivity, just use nuget to update this assembly and it will works :)
I made my first WPF control:
<UserControl x:Class="Dealogic.VisualStudio.UI.DatabaseManager.Controls.TargetInstance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<DataGrid ItemsSource="{Binding Customers}" />
</Grid>
On the DataGrid control it says:
"the type datagrid was not found, Verify that you are not missing an assembly refernece"
I'm creating the control within a Win Forms app. What assembly to I need to include and how do I do that in XAML?
thanks
Look up the control on msdn (I tend to google "msdn CONTROL_NAME"). On the page, it states the required assembly.
It depends on what version of WPF you are using.
In older version the Datagrid came from WPFTOOLKIT that you need to install.
In the new versions it's part of wpf.
microsoft.windows.controls
I've checked my code, here is a xaml for old datagrind:
<toolkit:DataGrid Margin="25,428,28,38" Grid.Column="2" ItemsSource="{Binding}" Name="grdEmails"></toolkit:DataGrid>
Here is one from VS2010:
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="152,59,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />
It works with default ref.
The other answers are fine (Googling to find the assembly, then ensuring you have a reference to it). However, there is another important piece of information on the MSDN page, and that is the target framework. DataGrid is available in .NET 4.0 but not .NET 3.5, for instance.