ListView带有文本和图标 - 添加ListView.Items.Add的项目,图标不会出现

时间:2014-02-22 14:31:38

标签: c# wpf

我有一个WPF控件:

<UserControl x:Class="MainToolWindowUI"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
    <DataTemplate x:Key="Template">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Path=Icon}"
                   Width="64"
                   Height="64"/>
            <TextBlock Text="{Binding Path=Message}"
                       VerticalAlignment="Center"/>
        </StackPanel>
    </DataTemplate>
    </UserControl.Resources>
    <ListView Margin="0" Name="listView" ItemTemplate="{StaticResource Template}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Severity" DisplayMemberBinding="{Binding Icon}"/>
                <GridViewColumn Header="Message" DisplayMemberBinding="{Binding Message}"/>
            </GridView>
        </ListView.View>        
    </ListView>
</UserControl>

我有一个列表视图项类,其中包含根据xml:

设置的绑定的字段
public class ListItem
        {
            public ListItem()
            {
            }

            public String Message
            {
                get {return "Message";}
            }

            public System.Drawing.Bitmap Icon
            {
                get
                         { 
                             Bitmap bitmap = new System.Drawing.Icon(SystemIcons.Information, SystemIcons.Information.Height, SystemIcons.Information.Width).ToBitmap();
                             return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height));
                     }
            }
        }

然后我添加这个项目:

listView.Items.Add(new ListItem());

消息显示正常,但我得到的只是文本“System.Windows.InteropBitmap”而不是图标。 Icon getter会被调用 我错过了什么?

2 个答案:

答案 0 :(得分:1)

您缺少指定绑定

<GridView>
  <GridViewColumn Header="Severity" DisplayMemberBinding="{Binding Icon}"/>
  <GridViewColumn Header="Message" DisplayMemberBinding="{Binding Message}"/>     
</GridView>

答案 1 :(得分:0)

public class itemClass
{
    public string VideoIcon { get; set; }
    public string VideoFile { get; set; }
    public string CreatedDateTime { get; set; }
}

 ListViewItem item = new ListViewItem();           
 this.listView.Items.Add(new itemClass { VideoIcon = @"E:\Self\ref.png", VideoFile = "filename", CreatedDateTime = "18/18/18" });
<!--code in xaml file-->
    <Grid>
        <ListView x:Name="listView">
            <ListView.View>
                
                <GridView>
                    <GridViewColumn x:Name="GridViewColumnName" Header="Name"  Width="35">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image  Width="20" Height="20" Source="{Binding VideoIcon}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn  Header="Video File" Width="70" DisplayMemberBinding="{Binding VideoFile}"/>
                    
                    <GridViewColumn Header="Created Date Time" Width="150" DisplayMemberBinding="{Binding CreatedDateTime}"/>
                </GridView>
                
            </ListView.View>
        </ListView>
    </Grid>