Search code examples
wpfbindingcommandlistviewitem

WPF Command Binding ListViewItem. Execute command when ListBoxItem clicked


It works (Command):

<Button Command="{Binding LoadMainCommand, Mode=OneTime}">
    <TextBlock Text="Testo" />
</Button>

And how to implement this here (Command) -> (ListViewItem)?:

<ListView>
    <ListViewItem>
        <StackPanel>
            <Image Source="../img.png">
        </StackPanel>
        <ListViewItem.ToolTip>
            <ToolTip Content="Testo" Style="{StaticResource tt_style}"/>
        </ListViewItem.ToolTip>
    </ListViewItem>
</ListView>

Solution

  • If you want to execute the command when the item is clicked (and not the content) the easiest would be to add an InputBinding to the ListBoxItem:

    <ListView>
      <ListView.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListBoxItem">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                  <Border.InputBindings>
                    <MouseBinding MouseAction="{x:Static MouseAction.LeftDoubleClick}"
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.SelectPageCommand}"
                                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=SelectedItem}" />
                  </Border.InputBindings>
    
                  <ContentPresenter />
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </ListView.ItemContainerStyle>
    </ListView>
    

    Alternatively turn the ListBoxItem into a Button:

    <ListView>
      <ListViewItem>
    
        <!-- You may need to adjust binding path -->
        <Button Command="{Binding LoadMainCommand, Mode=OneTime}">
          <StackPanel>
            <Image Source="../img.png">
          </StackPanel>
        </Button>
        <ListViewItem.ToolTip>
          <ToolTip Content="Testo" Style="{StaticResource tt_style}"/>
        </ListViewItem.ToolTip>
      </ListViewItem>
    </ListView>
    

    Alternatively override the ControlTemplate by setting the ListView.ItemContainerStyle.

    <ListView>
      <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListViewItem">
    
                <!-- You may need to adjust binding path -->
                <Button Command="{Binding LoadMainCommand, Mode=OneTime}"
                        Content="{TemplateBinding Content}" />
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </ListView.ItemContainerStyle>
      <ListViewItem>
        <StackPanel>
          <Image Source="../img.png">
        </StackPanel>
        <ListViewItem.ToolTip>
          <ToolTip Content="Testo" Style="{StaticResource tt_style}"/>
        </ListViewItem.ToolTip>
      </ListViewItem>
    </ListView>