Jake
Jake

Reputation: 103

Reading Data From WPF ListView. C#

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.

Upvotes: 2

Views: 969

Answers (1)

Sheridan
Sheridan

Reputation: 69979

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.

Upvotes: 1

Related Questions