0

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>
Stefan
  • 1
  • 2
  • if you need the click event then you may perhaps bind the `Command` of Button and use `CommandParameter="{Binding}"` that should do. – pushpraj Aug 07 '14 at 08:57
  • I don't _need_ the click event, using the buttons was just the easiest way, since I could reuse the style from the button that opens the popup. Also using `Command`and `CommandParameter` resulted in the same error. – Stefan Aug 07 '14 at 09:21
  • if you can see the correct name on button then setting `CommandParameter="{Binding}"` will push the full data item to the command, and `CommandParameter="{Binding Name}"` will send the value of Name property. Is it possible for you to share a working sample which reproduce the issue? May I try to fix for you. or you may at least post the command implementation and confirm that the command if being executed. I also notice that there is no ItemTemplate defined for ListBox in the DataTemplate, however a StackPanel seems to be its child (item). This look bit odd. – pushpraj Aug 07 '14 at 09:30
  • @pushpraj I'm not sure I can produce a working sample, since this is part of a not small project, also I would need to post at least several helper classes. I originally used [this data template](http://stackoverflow.com/questions/276808/how-to-populate-a-wpf-grid-based-on-a-2-dimensional-array), but then realized I could do the same with a `ListBox` and have the benefit of not needing a 2D input array and having an implemented `SelectedItem`. So maybe I butchered the xaml code a bit, but it still works. – Stefan Aug 07 '14 at 11:29
  • @pushpraj Based on my code above the implementation of your `Command` suggestion looks like above, only with the Button opening tag like this: ` – Stefan Aug 07 '14 at 11:33
  • Do also check if there are any binding errors, you can find the same in output window. also make sure the `SelectMode` property is public and of type `ICommand` or it's implementation and is initialized properly. – pushpraj Aug 07 '14 at 12:42

0 Answers0