XAML "Invalid Markup" error when using EventToCommand in VS2012 - c#

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 :)

Related

How to make checkbox trigger a command when checked?

I have a checkbox and a [RelayCommand] in the viewmodel that I want to link it to so that the command runs when the checkbox is ticked/unticked (ideally knowing the state of the checkbox as well) but I cannot seem to do it.
<CheckBox Grid.Column="1"
Grid.RowSpan="2"
Color="DarkGreen"
Scale="1.2">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior
EventName="CheckedChanged"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:AdvanceViewModel}}, Path=CheckAnswerCommand}"
CommandParameter="{Binding MarksFromPoint}" />
</CheckBox.Behaviors>
</CheckBox>
This is what I have tried currently but it gives me a System Exception: "Operation is not valid due to the current state of the object".
How do I make it work in the way I want.
I am using .NET 7 and using MAUI for the app. I have the Maui Community Toolkit in use as well.
Any help is appreciated.
I can replicate your issue and there's a Command is missing on Checkbox control #7394 on Github, you can follow up there.
Also, you can refer to EventToCommandBehavior throws InvalidOperationException in MAUI for checkbox which may provide a workaround to fix it.

How should I binding xaml event from WPF in .net core 3.0?

How should I binding a event in netcore 3.0?
In a WPF project(netcore3.0), there is no kind of Interactive.dll to do like
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=DoSomethingCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
so how to binding a event in WPF (netcore3.0)?
You should be able to use a mousebinding for this.
<YourControl.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="... />
</YourControl.InputBindings>
The way you're intended to use that dll now is via a nuget package - xaml bahaviors.
https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
Seeing as how net core 3 is still only in preview, i thought it might be rather early for this package to be updated.
Seems not though:
https://github.com/microsoft/XamlBehaviorsWpf/issues/13

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:
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>

Behaviors in UWP (Event trigger command error

SCENERIO:
I have a custom control where the control template has a grid. I want to add a behavior to the grid tapped event.
WHAT I HAVE DONE:
I have installed the Nuget package for managed UWP behaviors
Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed
In the custom control's resource dictionary, I have the following xml namespaces referenced along with other necessary namespaces
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
Then in the custom control, in the grid block, I have the following
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CustomCommand}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
ISSUES:
The core:InvokeCommandAction has the blue squiggly line with this error
The type EventTriggerBehavior does not
support direct content
STEPS TO REPRODUCE:
Create a custom control. Nothing special,maybe just a grid. Add the EventTriggerBehavior as shown above to the grid. Use the created control on a page in your application
This right here is where I'm stuck at. I've done a lot of Google searches and just can't find a way out of this error.
Any help and code correction will be appreciated
The core:InvokeCommandAction has the blue squiggly line with this error
The type EventTriggerBehavior does not support direct content
I made a basic demo and reproduced this problem. It is a XAML Designer issue. After building the project, this error is gone. So please make sure your codes are correct and go ahead build your project.

Without Interactivity.dll: Alternative to MVVMLight's EventToCommand

In our current C# project, we use the MVVM light framework and its EventToCommand feature to bind events to commands (as also mentioned in this SO thread).
We use it for example like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<cmd:EventToCommand CommandParameter="{Binding SelectedItem, ElementName=SelectTree}"
Command="{Binding Path=SelectTreeGotFocusCommand, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
My question is: What is the easiest way to achieve the same behaviour (i.e., EventToCommand, not limited to GotFocus) without using System.Windows.Interactivity?
To avoid the XY problem, here is some background:
We are developing an addon for an already existing main application, on which we do not have any influence. Our addon is served as a dll which is used by this main application.
So far, we used XAML code as given above (i.e., using System.Windows.Interactivity and EventToCommand). However, the last release of the main application contains an old version of System.Windows.Interactivity (so far, it was not included at all) which doesn't support this.
This means that using the old Interactivity.dll will cause our addon to be non-functional. However, overwriting the dll with the new version we need will cause issues with parts of the main application.
I read about using two different versions of the same dll in a project (for example here) but this seems to be quite involved. Since there are only a few places we use this construction, I thought that replacing it with code not using Interactivity.dll might be easier.
If you switch to the Microsoft.Xaml.Behaviors.Wpf nuget package and change your namespace declarations to
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
you can use the InvokeCommandAction like so:
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus"}">
<i:InvokeCommandAction Command="..." CommandParameter="..."/>
</i:EventTrigger>
</i:Interaction.Triggers>

Categories