1

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>
4
  • When do you expect your command to fire? When the item is selected? You'll need to use an interaction trigger to specify the event that you want to handle when there is no Command property. Commented May 8, 2020 at 14:25
  • But how do I make a trigger? To run this command when you click (ListViewItem) the mouse button. Commented May 8, 2020 at 14:35
  • I'm sorry, I didn't pay attention. Commented May 8, 2020 at 14:39
  • For anyone reading this in future, this may be helpful: learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/… Commented Jul 26, 2024 at 11:21

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, the first option is more suitable.

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.