c # - Tilføjelse af billede i ListView-kolonne

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg står over for problemet med at tilføje billede til en kolonne i ListView i wpf. Jeg har to kolonne sige x og y, og jeg vil gerne tilføje et billede i y-kolonnen. Jeg har prøvet meget. Min xaml er under ---


 `<ListView Name="listView1" ItemsSource="{Binding}" DataContext="{Binding}" SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Directory" DisplayMemberBinding="{Binding Directory}" />
                <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}" >                        
               </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
 `


og min c # kode er nedenfor


       public class FolderPath
    {
        public string Directory { get; set; }

        public System.Drawing.Image Status { get; set; }
    }

    private void button1\_Click(object sender, RoutedEventArgs e)
    {                    
 ObservableCollection<FolderPath> \_FolderCollection = new ObservableCollection<FolderPath>();
 BitmapImage b = new BitmapImage();
 b.BeginInit();
 b.UriSource = new Uri(@"C:UserssmkDocumentsVisual Studio 2010ProjectsFolder\_lockerFolder\_lockerfolder\_lock.ico");
 b.EndInit();

 System.Windows.Controls.Image i= new System.Windows.Controls.Image();
 i.Height = 20;
 i.Source = b;

 System.Drawing.Image.FromFile(@"C:UserssmkDocumentsVisual Studio 2010ProjectsFolder\_lockerFolder\_lockerfolder\_lock.ico");


      listView1.Items.Add(new FolderPath { Directory = "something", Status = System.Drawing.Image.FromFile(@"C:UserssmkDocumentsVisual Studio 2010ProjectsFolder\_lockerFolder\_lockerfolder\_lock.ico") });                  
    }


det viser noget i første kolonne, men viser ingenting i 2. kolonne.
Jeg har forsøgt både system.windows.controls.image og system.drawing.image begge, men det viser ikke billede. hvad kan jeg gøre?

Bedste reference


Prøv dette:


public class FolderPath
{
    public string Directory { get; set; }
    public string Status { get; set; }
}

<GridViewColumn Header="Status">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=Status}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>


private void button1\_Click(object sender, RoutedEventArgs e)
{                    
    ObservableCollection<FolderPath> \_FolderCollection = new ObservableCollection<FolderPath>();

    listView1.Items.Add(new FolderPath { Directory = "something", Status = @"C:UserssmkDocumentsVisual Studio 2010ProjectsFolder\_lockerFolder\_lockerfolder\_lock.ico" } )
}

Andre referencer 1


Prøv et CellTemplate


http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumn.celltemplate.aspx[4]