选择时更改 ListView 图像项目的源
下面是我所追求的示例:
我决定使用ListView(尝试了基于选择器的自定义控件,但我无法输出任何令人满意的内容)。
我的列表显示正常,但我正在努力寻找在选择该项目时如何更改图像源。这是我的代码:
<UserControl.Resources>
<DataTemplate x:Key="PagingIndicatorTemplate">
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/MyProject;component/Resources/Images/ic_paging_button_normal.png" />
<!-- I guess that's where I need to put my stuff to change the image ? ... -->
</Style>
</Image.Style>
</Image>
</DataTemplate>
</UserControl.Resources>
<ListView Name="PagingIndicator"
Height="30"
ItemTemplate="{DynamicResource PagingIndicatorTemplate}"
ItemsSource="{Binding Path=News}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Below is a sample of what I am after:
I have decided to implement that using a ListView (tried a custom control based on Selector but I could not managed to output anything satisfying).
My list displays fine but I am struggling to find how to change the image source when the item gets selected. Here is my code:
<UserControl.Resources>
<DataTemplate x:Key="PagingIndicatorTemplate">
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/MyProject;component/Resources/Images/ic_paging_button_normal.png" />
<!-- I guess that's where I need to put my stuff to change the image ? ... -->
</Style>
</Image.Style>
</Image>
</DataTemplate>
</UserControl.Resources>
<ListView Name="PagingIndicator"
Height="30"
ItemTemplate="{DynamicResource PagingIndicatorTemplate}"
ItemsSource="{Binding Path=News}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
</ListView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一件事是错误的...您正在使用
ListView
但将样式定位为ListBoxItem
。它应该是ListViewItem
。在
Image
的样式中使用 DataTrigger,检查RelativeSource
ListViewItem
和Path=IsSelected
上的绑定(如果为 True)并更改图像的Source
。There is one thing wrong... you are using
ListView
but targetting style toListBoxItem
. It should beListViewItem
.In
Image
's style use a DataTrigger where check the binding onRelativeSource
ListViewItem
andPath=IsSelected
(if its True) and change theSource
of the image.我决定这样解决这个问题:
I have decided to solve the problem like that: