简体   繁体   English

每个项目用相同的图标绑定ListView(WPF)的最佳方法是什么

[英]what is the best way to bind ListView (WPF) with same icon for each item

I have a list view that is binded to some object I've created. 我有一个绑定到我创建的对象的列表视图。 The binding is working perfect, but I want to add icon to each item in my list. 绑定工作正常,但是我想向列表中的每个项目添加图标。 All the items should have the SAME icon (should be defined prior as "file.ico"). 所有项目均应具有SAME图标(应先定义为“ file.ico”)。 What is the best to implement this? 什么是最好的实现呢? Thanks. 谢谢。

Use an ItemTemplate , eg: 使用ItemTemplate ,例如:

<ListBox ItemsSource="{Binding MyItems}">
   <ListBox.ItemTemplate>
      <DataTemplate>           
         <Grid>
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="Auto" />
               <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="/Images/.." Width=".." Height=".." />
            <TextBlock Grid.Column="1" Text="{Binding Name}" />
         </Grid>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

You should use a ListBox unless you have a specific reason to use a ListView . 除非有特殊原因要使用ListView否则应使用ListBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF-将数据从数据库绑定到复选框控件的最佳方法是什么 - WPF - what is the best way to bind data from a database to a checkbox control 解除绑定WPF ListView的最佳方法 - Best way to unbind wpf listview 只需单击 ListView 中的项目即可在 ViewMomdel 中启动函数的最佳方法是什么 - What is the best way to start a function in a ViewMomdel just by clicking on the item in the ListView 将HashSet绑定到ListView项(C#,WPF) - Bind HashSet to a ListView Item (C#, WPF) 在WPF用户控件中为选定项目实现图形的最佳方法是什么? - What is the best way to implement the graphics for a selected item in a WPF user control? 在列表中的每个项目中搜索匹配的最佳方法是什么? - What is the best way to make a search for match at each item in a list? 将LINQ to Entities查询中的每个项目转换为接口的最佳方法是什么? - What is the best way to cast each item in a LINQ to Entities query to an interface? 将DataGrid绑定到WPF中的通用列表的最佳方法 - Best way to bind DataGrid to generic list in WPF 同一台机器上的两个程序相互通信的最佳方法是什么 - What is the best way for two programs on the same machine to communicate with each other 将一组相同类型的ViewModel绑定到MVVM / WPF中的TabControl的最佳方法 - Best way to bind a set of same-type ViewModels to a TabControl in MVVM / WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM