I have created EventTrigger for checkbox check and uncheck event which is placed inside Grid in one View( view model name :EventViewModel) which is using Caliburn.Micro mvvm.
Code:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal="http://www.caliburnproject.org"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cal:ActionMessage MethodName="SelectedFile_Checked">
<cal:Parameter Value="{Binding Name}"/>
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
We have used Conductor.Collection.AllActive in MainHomeWIndowViewModel and activate EvenView by using below code
Items.Add(new EventViewModel());
Problem is when SelectedFile_Checked event triggering its always locating into MainHomeWIndowViewModel and its not reaching into EventViewModel. It should be always locate into method(SelectedFile_Checked ) inside EventViewModel.
Any idea to solve this issue!
Related
I have installed MVVMLIGHT but still somehow I can't access EventToCommand property even after adding the reference files and using the namespaces in xaml file
xmlns:i="http://schemas.microsoft.com expression/2010/interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight;assembly=GalaSoft.MvvmLight"
<Textbox x:name="quantity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
//not working <cmd:EventToCommand Command="{Binding quantityCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Textbox>
Even InvokeCommandAction would work but I m not getting that too.Plz show me.thank you in advance
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>
I am using Silverlight 5 and in a view I have a Button that, when clicked, loads a particular state. This is done using an EventTrigger and the GoToStateAction markup like so:
<Button x:Name="..." Content="...">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:GoToStateAction StateName="MyState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
The above works swimmingly, but if I move the Button into a DataGridTemplateColumn it no longer works. Specifically, the app compiles without error and the DataGrid shows the button in a column, but when I click the button the state is not transitioned to.
<data:DataGrid ...>
<data:DataGrid.Columns>
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="..." Content="...">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:GoToStateAction StateName="MyState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
Is it not possible to have EventTriggers in a DataGridTemplateColumn? Or do I need to declare them using alternate syntax?
Thanks
The GoToStateAction reference states that, if no target is specified, it will walk up the visual tree and apply the state change on the first element it finds that defines states. In your second case, I believe that some elements of the DataGrid visual tree (cells, rows, etc) may be taken in account before your usercontrol, thus the behavior you've seen.
Since you are using Silverlight 5, you can try to set the TargetObject property of the action to your desired control, using a binding with a relative source looking of a ancestor of the type of UserControl.
<ei:GoToStateAction StateName="MyState" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/>
Trying to bind the click or checked event on a WPF chekbox to a command in my viewmodel, but im unsure on the technique and the event names, can anyone point me in the right direction?
For now the code compiles but the the trigger does not call the FooCommand
<CheckBox IsChecked="{Binding PartData.ReportIncluded, Mode=TwoWay}"
VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<i:InvokeCommandAction Command="{Binding FooCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
Looks like you're missing the "On" prefix of your event, i.e. OnMouseLeftButtonDown or OnPreviewMouseLeftButtonDown
I know you'll use this to invoke method on the ViewModel, but just out of curiosity, how would you use to wire an event handler on the instance of the view, equivalent to wiring it in code behind.
The TargetObject is the object which has the method to be invoked, if you have the method in your window code-behind then the object is the window itself. You can bind to it by its name:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
...etcetera...
x:Name="UserControl">
So your CallMethodAction would be:
<ei:CallMethodAction MethodName="MyMethod"
TargetObject="{Binding ElementName=UserControl, Mode=OneWay}"/>
I think you are probably after something like this (ignore that this specific example is best done with a command).
<Button Content="Click this">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="SomeMethod" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>