Привязка команд WPF ListViewItem. Выполнить команду при нажатии ListBoxItem - PullRequest
0 голосов
/ 08 мая 2020

Работает (Команда):

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

А как это реализовать здесь (Команда) -> (ListViewItem)?:

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

1 Ответ

0 голосов
/ 08 мая 2020

Если вы хотите выполнить команду при щелчке по элементу (а не по содержимому), проще всего будет добавить InputBinding к 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>

Или поверните ListBoxItem в 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>

В качестве альтернативы переопределите ControlTemplate, установив 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>
...