0

I have to add an image to a listview. I'm not able to add it in the XAML and I can't figure out how to get an image in the CollectionView. I'm very new to C#/WPF.. Be gentle...

        public class TexterListItem
        {
            public string Icon { get; set; }
            public string Text { get; set; }
            public string FilePath { get; set; }
        }

        public void GetTexters()
        {
            List<TexterListItem> texters = new List<TexterListItem>();

            string userTexterDirectory = Directory.GetCurrentDirectory() + "\\Texter\\" + Environment.UserName;

            if (Directory.Exists(userTexterDirectory))
            {
                string[] fileEntries = Directory.GetFiles(userTexterDirectory);
                foreach(string file in fileEntries)
                {
                    FileInfo fileInfo = new FileInfo(file);

                    texters.Add(new TexterListItem() { Icon = "IMAGE", Text = fileInfo.Name.Replace(fileInfo.Extension, ""), FilePath = fileInfo.FullName });
                }
                LVTexter.ItemsSource = texters;
                texterView = (CollectionView)CollectionViewSource.GetDefaultView(LVTexter.ItemsSource);
                texterView.Filter = UserFilter;
            }
            else
            {
                Directory.CreateDirectory(userTexterDirectory);
            }
        }

Here is the XAML.

<ListView x:Name="LVTexter" Grid.Row="5" Grid.RowSpan="1" Grid.Column="4" Grid.ColumnSpan="1" Height="170" Margin="5 0 5 0" MouseDoubleClick="LVTexter_DoubleClick">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Placeholder"></MenuItem>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Copy" Width="50" DisplayMemberBinding="{Binding Icon}"/>
                    <GridViewColumn Header="Texter" Width="500" DisplayMemberBinding="{Binding Text}"/>
                </GridView>
            </ListView.View>
        </ListView>
  • 1
    Post your Xaml. There are many ways to display an image inside a ListView. A simple search found this https://stackoverflow.com/a/35786257/1966993. Although I suggest there are better approaches than using Bitmap image. You can work with paths as well. – G K Feb 23 '22 at 21:06
  • Thanks. I'll give that a shot. I guess I wasn't sure what to search for. – Tanner Bryant Feb 23 '22 at 21:13

0 Answers0