2

How to read data from a WPF ListView?

Here is my code.

        <ListView x:Name="LVR"  AllowDrop="True" PreviewDrop="LVR_PreviewDrop" RenderTransformOrigin="0.505,0.506" Margin="0,0,0,0" Grid.Row="1" Grid.ColumnSpan="3" MouseEnter="LVR_MouseEnter" >
        <ListView.View>
            <GridView >
                <GridViewColumn Header="Status" Width="40">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="index.png" Width="26"></Image>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="File Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding TBtxt}" FontWeight="Bold"  Foreground="Blue" Cursor="Hand" Height="30" TextAlignment="Left" HorizontalAlignment="Center"></TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

And i am inserting items to the List view like this.

void Insert()
{
            WinForms.OpenFileDialog ofd = new WinForms.OpenFileDialog();
        ofd.Multiselect = true;
        ofd.Title = "Select .TXT File";
        ofd.FileName = "";
        ofd.Filter = "TXT | *.txt";
        if (ofd.ShowDialog() == WinForms.DialogResult.OK)
        {
            foreach (var filename in ofd.FileNames)
            {
                if (System.IO.Path.GetExtension(filename).ToUpperInvariant() == ".txt")
                {
                      LVR.Items.Add(new StackItems { TBtxt = filename });
                }
            }
        }
}
class StackItems
{
    public string TBtxt { get; set; }
    public Image imgg { get; set; }
}

Once I completed adding files, my ListView will looks like this.

|Status | File Name|

|[Image]| test.txt |

|[Image]| test1.txt|

(Sorry. I don't have enough reputation to post image)

Now how will I read the 'File Name' from second column?

I am very new to WPF. Thanks in advance.

2
  • 1
    Not sure what exactly you mean with "read from the ListView", but you would usually access your StackItems class (aka your view model), and get file names from its TBtxt property (which should have a better name). Instead of adding StackItems instances to the ListView's Items collection and then forgetting them, your would add them to an ObservableCollection<StackItems> and data-bind the ListView's ItemsSource property to that collection. Search the web for MVVM. Commented Feb 25, 2015 at 12:44
  • Note also that you wouldn't usually use the type Image in a WPF application, as it is from WinForms. Use ImageSource instead. Commented Feb 25, 2015 at 12:49

1 Answer 1

1

In short, you should be data binding a collection of items (one for each row) to the ListView.ItemsSource property:

<ListView ItemsSource="{Binding SomeCollection}">
    <ListView.View>
        <!-- Define your view here -->
    </ListView.View>
</ListView>

If you do this, then accessing the items is as simple as this (using Linq):

var firstItem = SomeCollection.First();

An improvement on this situation would be to data bind another property of the same type as the objects on the data bound collection to the ListView.SelectedItem property:

<ListView ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding CurrentItem}">
    <ListView.View>
        <!-- Define your view here -->
    </ListView.View>
</ListView>

Doing this will enable you to access properties from the currently selected item from the ListView like this:

int someValue = CurrentItem.SomeProperty;

Please refer to the ListView Class page on MSDN for further help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.