EventToCommand in ItemControl in Windows Phone 8 - silverlight

I cannot understand why I cant call eventToCommand in my datatemplate inside ItemControl. According to this post I should implement it in the dataTemplate, but the command is never invoked. This is the eventToCommand im trying to insert in my code.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding ItemSelectedCommand, Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
And this is the code im trying to instert it to. However, as the comments say, it is never invoked when put in the dataTemplete. The problem is not the viewModel, the command works in the panelTemplate.
<ItemsControl ItemsSource="{Binding GroupRow1}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<!-- COMMAND WORKS HERE, but cannot locate which item has been pressed -->
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="#FF1756C3" Height="173" Width="173" Margin="12,0,0,0" >
<!-- COMMAND DOES NOT WORK HERE?!?! -->
<StackPanel>
<TextBlock Text="{Binding LineOne}" /> <!-- These bindings work -->
<TextBlock Text="{Binding LineTwo}" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
How do I find out which Item has been pressed?

Many answers, but none of them worked for me. I redesigned my solution and got rid of the eventtocommand which wasent working. Instead I made buttons with custom content to look the same as my border.
Simpler code and a better solution.
<ItemsControl ItemsSource="{Binding GroupRow1}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button CommandParameter="{Binding LineOne}" Height="195" Width="195" Margin="0,0,-10,0" Click="Button_Click_2" Background="#FF1756C3">
<StackPanel>
<TextBlock Text="{Binding LineOne}" />
<TextBlock Text="{Binding LineTwo}" />
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

I had a similar problem before, what solved it for me was referencing the VM in the binding. Try to set PassEventArgsToCommand to false if you want to receive the item instead of the EventArgs
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="False"
CommandParameter="{Binding}"
Command="{Binding Path=VM_Name_Here.Command_Name_Here, Source={StaticResource Locator}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Edit- If you are using MVVM Light, in the app.xml you should have something like:
xmlns:vm="clr-namespace:PhoneApplication.ViewModel (namespace where your ViewModelLocator is under)
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="true" />
You are telling the binding to look in the ViewModelLocator for a particular VM.
Hope it helps,
Regards.

I think it has to do with your binding in the data template. How is the view created? I think the command is a property of the viewModel, not GroupRow1 which is the collection for your items control. You need to bind to the command in your ViewModel. SO if your view is a usercontrol, the following should work. (if its of another type then change the ancestortype)
<cmd:EventToCommand Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.ItemSelectedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock}},Path=Name}"/>
The command parameter adds the name of the textblock for example, you could change that to any property of the textblock.
I would personally have SelectedItem property in my viewmodel that can be accesses as an object from the ItemSelectCommand
Hope this helps.

The answer is pretty obvious. In ItemsPanelTemplate your binding source is still the ViewModel and your command stays at your ViewModel. But in DataTemplate you are iterating over GroupRow1 items and your binding source is individual item. If you want to use your command there, you have to bind from the relative source in example:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding ViewModel.ItemSelectedCommand, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>

Related

How to set MouseBinding in ItemsControl

I want to call click command on TextBlock, and i do that like this
<TextBlock Text="New Project">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding Path=NewProject}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
And that is work, but i want to do the same in ItemsControl, so
<ItemsControl Margin="8" ItemsSource="{Binding Path=Projects}" Grid.Row="1" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" Style="{StaticResource ClickableTextBlock}" >
<TextBlock.InputBindings>
<MouseBinding Command="{Binding Path=OpenTasks}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And that doesn't work, command is not called. How to do that?
The implicit DataContext inside ItemTemplate is the current data item. But the OpenTasks is not part of each data item. As you said in your comment, it's part of your viewmodel. So in this case you have to set some explicit source with RelativeSource for the Binding. It helps walk up the visual tree and target the source you want. In this case you need the DataContext of the ItemsControl. The code should be like this:
<MouseBinding Command="{Binding DataContext.OpenTasks,
RelativeSource={RelativeSource AncestorType=ItemsControl}}"
MouseAction="LeftClick"/>

access method from template in wpf

I'm using WPF with the caliburn micro framework to implement a MVVM pattern.
I've created a popup that gets filled with custom buttons inside a ListBox.
Now I want to call a method in my ViewModel when one of these buttons is clicked, but each approach I tried so far failed.
Here the code in comments works in the sense that it calls my method, but the parameter is always null.
<ListBox x:Name="lst" ItemsSource="{Binding OperatingModes}" ItemTemplate="{DynamicResource DataTemplate_Level1}" BorderThickness="0" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown" >
<cal:ActionMessage MethodName="SelectMode">
<cal:Parameter Value="{Binding ElementName=lst, Path=SelectedItem}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>-->
</ListBox>
And this is the Template I'm using. Whenever I call this method, I'm getting "No target found for method SelectMode." As you can see, I've tried different aproaches, although I'm not sure that I used the TargetWithoutContext call properly.
As far as I can tell I need to somehow bind my template to the data context of the "normal" xaml code, but I failed so far. How do I access my method properly?
<DataTemplate x:Key="DataTemplate_Level1" x:Name="myListTemplate" >
<ListBox ItemsSource="{Binding}" BorderThickness="0" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" cal:Bind.Model="{Binding}" cal:Action.TargetWithoutContext="{Binding DataContext, ElementName=lst}">
<Button Style="{StaticResource InformButton}" Content="{Binding Path=Name}" FontSize="11" BorderBrush="BlueViolet" cal:Message.Attach="SelectMode($dataContext)">
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonDown" >
<cal:ActionMessage MethodName="SelectMode">
<cal:Parameter Value="{Binding ElementName=myListTemplate, Path=SelectedItem.Name}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>-->
</Button>
</StackPanel>
</ListBox>
</DataTemplate>

silverlight combobox invokecommandaction on selectionchanged event cannot pass parameter

I have a combobox, using databinding and MVVM pattern. Everytime the user changes the selection, I added an event trigger, and a command is executed. the code is the following:
<ComboBox x:Name="myComboBox" Width="150" ItemsSource="{Binding Items}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding LoadCommand}" CommandParameter="{Binding SelectedItem, ElementName=myComboBox}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
the problem is that the parameter it passes is always null.
I changed the parameter from SelectedItem to SelectedIndex, this way I get the parameter and it is not null. Then i get my object using the index.

RadTreeViewItem, MVVM, and Click events

I'm having trouble getting a click event or mouse down event to fire on a RadTreeViewItem in the ViewModel. What syntax should I be using? This is the relevant XAML below:
<Toolkit:AccordionItem x:Name="Accordion1" Header="{Binding Header, Mode=TwoWay}" Width="200">
<ListBox x:Name="SitesList" Width="195" BorderThickness="0" ItemsSource="{Binding Games, Mode=OneWay}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<telerik:RadTreeView IsDragDropEnabled="True" IsSingleExpandPath="True"
telerikDragDrop:RadDragAndDropManager.AllowDrag="True" PreviewDragEnded="RadTreeView_PreviewDragEnded"
IsDragTooltipEnabled="False">
<telerik:RadTreeViewItem Header="{Binding siteName, Mode=TwoWay}" Tag="{Binding siteKey, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding RadTreeItemClickCommand, Mode=TwoWay}" MustToggleIsEnabledValue="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadTreeViewItem>
</telerik:RadTreeView>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Toolkit:AccordionItem>
As far as I can tell, binding the events of the RadTreeViewItem has to be done in code-behind; the ItemPrepared event of RadTreeView is fired when a RadTreeViewItem is created and bindings can be set in a handler. More information here:
http://www.telerik.com/help/silverlight/radtreeview-events-working-with-item-prepared-event.html
Don't bother with the event to command. You can set a command on the RadTreeViewItem directly.

mvvm light PassEventArgsToCommand break app in ListBoxDragDropTarget

Hi I'm trying to pass eventargs to comman that I bind in ViewModel. Everything works fine if I don't use PassEventArgsToCommand="True". But If put PassEventArgsToCommand="True" than app break. Did you have similar problem?
<Controls:ListBoxDragDropTarget Grid.Column="1" AllowDrop="true" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PersonDrop, Mode=OneWay}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox x:Name="fromListBox" ItemsSource="{Binding Person, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, Mode=TwoWay}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Controls:ListBoxDragDropTarget>
Ok. I found out. I used System.Windows.DragEventArgs instead of Microsoft.Windows.DragEventArgs as argument in my command.

Resources