它的工作原理是(Command)。
<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>
如果你想在点击项目时执行命令(而不是内容),最简单的方法是在项目被点击时添加一个 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>