选择时更改 ListView 图像项目的源

发布于 2024-12-06 15:49:51 字数 1653 浏览 1 评论 0原文

下面是我所追求的示例:

The Paging Indicator I would like to重现

我决定使用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:

The paging indicator I would like to reproduce

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人事已非 2024-12-13 15:49:51
  1. 有一件事是错误的...您正在使用 ListView 但将样式定位为 ListBoxItem。它应该是 ListViewItem

  2. Image 的样式中使用 DataTrigger,检查 RelativeSource ListViewItemPath=IsSelected 上的绑定(如果为 True)并更改图像的 Source

  1. There is one thing wrong... you are using ListView but targetting style to ListBoxItem. It should be ListViewItem.

  2. In Image's style use a DataTrigger where check the binding on RelativeSource ListViewItem and Path=IsSelected (if its True) and change the Source of the image.

新一帅帅 2024-12-13 15:49:51

我决定这样解决这个问题:

  • 制作两个不同的项目模板(每个模板都有自己的图像、鼠标悬停的样式、按下的鼠标...)
  • 使用 IsSelected 属性上的触发器更改 ListViewItem 的 Template 属性。

I have decided to solve the problem like that:

  • Make two different item templates (each with their own images, styles for mouse over, mouse pressed, ...)
  • Change the Template property of the ListViewItem with a trigger on its IsSelected property.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文