【问题标题】:WPF Command Binding ListViewItem. Execute command when ListBoxItem clickedWPF 命令绑定 ListViewItem。单击 ListBoxItem 时执行命令
【发布时间】:2020-05-08 14:20:01
【问题描述】:

有效(命令):

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

以及如何在此处(Command)->(ListViewItem)实现?:

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

【问题讨论】:

  • 你希望你的命令什么时候触发?什么时候选择项目?当没有Command 属性时,您需要使用interaction trigger 来指定要处理的事件。
  • 但是我该如何触发呢?单击 (ListViewItem) 鼠标按钮时运行此命令。
  • 对不起,没注意。

标签: wpf binding command listviewitem


【解决方案1】:

如果您想在单击项目(而不是内容)时执行命令,最简单的方法是将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>

通过设置ListView.ItemContainerStyle 或者覆盖ControlTemplate

<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>

【讨论】:

  • 非常感谢,第一种比较合适。
猜你喜欢
  • 2016-08-21
  • 2011-07-29
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
相关资源
最近更新 更多