Disable Triggers in Design Time - wpf

Im developing a WPF Application with a TabControl. Inside the TabItems of this TabControl I have implemented EventTriggers which react on a LeftButtonMouseDownEvent to do some stuff. My Problem is that during Design Time of Visual Studio the Event is triggered and it seems to block any further interactions in the Design Window.
Is there a possibility to ignore the trigger, when my Visual Studio is in Design Time. I would prefer to do this in pure XAML. I know that there exists the "mc:Ignorable" tag, but I want it the other way round. If needed i posted a Code Snippet for you.
Thanks for your help!
<TabItem>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<SomeAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TabItem>

I don't know how to do it in xaml, but, may be, adding to code SomeAction this, will resolve your problem.
var isInDesignMode = DesignerProperties.GetIsInDesignMode(new DependencyObject());
if(isInDesignMode)
{
// using in VisualStudio or Blend
}
else
{
// using in application
}

Try extending mc:Ignorable with i for your case, i.e. mc:Ignorable="d i":
<UserControl ...
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
d:DesignHeight="600"
d:DesignWidth="800"
...
mc:Ignorable="d i">
...
<TabItem>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<SomeAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TabItem>
...
</UserControl>

Related

MVVM Light, Mahapps metro and EventToCommand via EventTrigger from System.Windows.Interactivity

I'm going nuts on this one:
I have an application using both Mahapps.Metro as well as MVVMLight.
Basically most things are ok UNTIL you try to use Interaction.Triggers.
I typically end up with errors like
Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.
A way to reproduce this is via a repo I found online:
https://github.com/mike-schulze/mahapps-mvvmlight-setup
(sorry mike for hijacking) and adding a metro SplitButton to the viewmodel:
<Controls:SplitButton ItemsSource="{Binding MyList}" Grid.Column="1" Grid.Row="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Controls:SplitButton>
And MyList being
public List<string> MyList
{
get
{
List<string> theList = new List<string>();
theList.Add("Hello");
theList.Add("World");
return theList;
}
}
I'm not sure how many different namespaces, hard coded Interactivity versions in App.config or what so ever I tried.
Has anyone a working example with Mahapps Metro (version not too old) and MVVM Light and the EventToCommand stuff?
I thought my problem is related to updates to Metro - and them using Behaviors now instead of Interactivity. I'm using the Interactivity dll version 4.5.0.0.
But even the old git I mentioned above shows the problem... And I can't find a working solution.
Any help is greatly appreciated.
Thanks
You need to add the namespace for System.Windows.Interactivity too.
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Because MVVMLight uses this version of Interactivity.
<mah:MetroWindow x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
...
DataContext="{Binding Main, Source={StaticResource Locator}}">
...
<mah:SplitButton ItemsSource="{Binding MyList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<command:EventToCommand Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</mah:SplitButton>
...
</mah:MetroWindow>

WPF mvvm wizard

I am implementing WPF MVVM Wizard and I am wondering about the right approach of performing a DoOperation when a new Wizard’s Page (UserControl) is loaded.
The DoOperation is implemented on the MyWizard.ViewModal class while the UserControl Load is happening at the MyWizard.View namespaces.
How can I connect between the UserControl loaded event to the DoOperation api?
I tried the following:
<xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=RunOperation}"/
</i:EventTrigger>
</i:Interaction.Triggers>
RunOperation calls DoOperation.
it doesn’t work, RunOperation is not being called.
This is the right approach or there is a better way to perform an operation at the MyWizard.ViewModal class?
Your approach should work. Have you checked your output console for binding errors? Is RunOperation a command? Is the DataContext of the UserControl already set, when the Loaded event is raised? Have you implemented the triggers like this in your UC?
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=RunOperation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
...
</Grid>
</UserControl>

Routed Events of TextBlock

How to fire RoutedEvents of TextBlock in ViewModel from code behind in wpf.
Kindly let me know, how I can bind routed events in wpf code behind. Thanks
Well I use this (you will need System.Windows.Interactivity in Silverlight, but I suspect it's similar in WPF):
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<TextBlock Text="{Binding InputValue, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding InputValueGotFocusCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
You can do this easy in Blend

Bind a ICommand to a WPF tabcontrol/tabitem using XAML (MVVM)

I have a WPF 3.5 app built using MVVM pattern. I have a tabcontrol and I want to excecute an ICommand on the view model when the user clicks a certain tab (jn my case the "Preview" tab).
How do I hook up my ICommand to the tabitem or tabcontrol using just the XAML? I am using MVVM so I dont want to use events and get filthy, dirty code in the code-behind.
I think Im missing something simple here!
Thanks,P
Please refer this article : hooking up commands to events
Or you can use this approach
You should use InvokeCommandAction, Looks like this:
<TabItem Header="TabItem">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="SomeCommand"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid />
</TabItem>
Be sure to include the xmlns in the top:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
and of course to reference System.Windows.Interactivity (new in WPF4)

WPF/Silverlight States - Activate from XAML?

Kind of a quick question:
Is it possible to activate a viewstate from XAML? I have been only able to activate one from CS, using the VisualStateManager.GotoState() method. This would fix some of my MVVM issues if it were easily possible.
Thanks
If you are familiar with Blend behaviors, triggers, and actions there is a GoToStateAction which is a part of the Microsoft.Expression.Interactivity.Core namespace. You will have to reference the interactivity assemblies which are part of the Blend SDK.
Once you have the references set up it's as easy as specifying the GoToStateAction to react to some sort of trigger... all in XAML. Here is an example which fires the action off of the Loaded event using an EventTrigger:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
<Grid x:Name="LayoutRoot">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<ic:GoToStateAction StateName="MyVisualState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</Grid>
</UserControl>
More info and tutorial about the specific GoToState action here.
EDIT: This answer is specific to Silverlight, not sure if this is available in WPF.

Resources