【发布时间】:2016-03-10 12:19:01
【问题描述】:
问题
我有一个显示图像列表的列表框。我希望能够将鼠标悬停在图像上并让它以更大的尺寸显示图像。我能够获得悬停效果并让它显示我输入的一些文本,但我需要为不同的 ListBoxItems 更改图像。
代码
这是我用来获得悬停效果的代码:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="5,0,5,5" />
<Setter Property="Height" Value="140"/>
<Setter Property="Width" Value="200"/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="ToolTip" Value="{Binding IMAGEHERE}"></Setter>
<Setter Property="Control.Background" Value="#d64b36" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
如您所见,我有工具提示,但我需要将图像绑定到 ViewModel 中的值。
如果当我将鼠标悬停在 ListBoxItem 上时可以触发 ViewModel 中的事件,它应该可以解决我的问题。
【问题讨论】:
-
<Setter Property="ToolTip"> <Setter.Value> <Image Source="{Binding Imagepath}" /> </Setter.Value> </Setter> -
@Gopichandar 如何在我的 ViewModel 中触发方法来设置图像路径?
-
您的虚拟机中应该有一个名为
Imagepath的属性,它应该保存图像的路径。 -
@Gopichandar 但是每个 ListBoxItem 都有不同的图像,因此属性需要根据图像进行更改。
-
当然应该。您将拥有
List<items>,每个item应包含Imagepath
标签: wpf binding listbox viewmodel listboxitem