0

I have a stackpanel that contains many elements including buttons. I am not able to fire a method through ICommands. It works through adding an event handler in the code behind for the Button.Click event. Since, I am following a MVVM pattern, I want to fire a command. Also, this stackpanel is a datatemplate for my listviewitem.

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" IsHitTestVisible="True">
            <Button Cursor="Hand" Command="{Binding MoveImageUp}">
               <Button.Background>
                   <ImageBrush ImageSource="..."/>                                      
               </Button.Background>
            </Button>
            <Grid>
                 <Image Source="{Binding Path=Image}"/>
                 <TextBlock Height="20" Width="20" Text="{Binding Path=Order}" Opacity="0.8" HorizontalAlignment="Left" VerticalAlignment="Top"/>
             </Grid>
             <GridSplitter HorizontalAlignment="Center" Padding="1" />
        </StackPanel>
    </DataTemplate>
 </ListView.ItemTemplate>

I have tried setting IsHitTestVisibile to true, does not work.

9
  • 1
    I assume you command is in one level up, so you have to specify correct context for binding. Commented May 28, 2018 at 13:48
  • 1
    stackoverflow.com/questions/14056939/… Commented May 28, 2018 at 13:50
  • 1
    stackoverflow.com/questions/29954550/… Commented May 28, 2018 at 13:50
  • if it was a problem just close your Q as duplicate (do not remove it) Commented May 28, 2018 at 13:50
  • Thanks. That worked. Commented May 28, 2018 at 13:56

1 Answer 1

1

There was no DataContext set for the button. Thanks @Foggy Finder

<Button Cursor="Hand" Command="{Binding ElementName=ImagesList, Path=DataContext.MoveImageUp}" CommandParameter="{Binding}">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.